mod_rewrite rewriting all folders - mod-rewrite

I'm trying to use mod_rewrite to create clean links. I've gotten it to take my urls from this:
http://www.example.com/index.php?LTKeywords=long-tail-keyword
to
http://www.example.com/long-tail-keyword/
Which is exactly what I want. The code I used is:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/ index2.php?LTKeywords=$1
One Problem.
It now redirects all of my folders, so anything in my /images/ or /css/ folders will not show up. Is there a way to stop it from rewriting specific folders?

The easiest way is probably a RewriteCond. You can prefix with ! to specify must-not-match:
RewriteCond %{REQUEST_URI} !^/css
for example.
You may also just want to exclude directories which actually exist:
RewriteCond %{REQUEST_URI} !-d
Finally, you could also put your static content on a different hostname (virtualhost), avoiding your rewrite rules all together.
You could also add some more RewriteRules to match your CSS, etc. and make them not actually change anything (rewrite /css to /css) but specify the L (last) flag, so the other rules wouldn't run.

Related

rewrite base URL and add a specific word to it using mod_rewrite

below is my requirement :
Supposing i have a URL coming from browser
http://test.aroa.com/mance
I need to add /xyz/abc/xy/ before perfomance and .html after it i.e. using re-write rules of mod_rewrite to change it to below URL once it hits the dispatcher in AEM(or apache web-server)
http://test.aroa.com/xyz/abc/xy/mance.html
For this i wrote the below re-write rule along with some rewrite conditions(rewrite conditions not here)
RewriteRule ^(/.*)$ /xyz/abc/xy$1.html [P,L]
It works for the this site but messes up some other functionalities by adding /xyz/abc/xy to other URLs as well
Can you suggest me some way using which i can restrict the URL rewriting only for the //test.aroa.com/ URL and not affect any other URL
I tried putting the rule inside directory tag of with the doc-root name inside it. but it fails to get applied in that case..
Can anyone suggest something that can help
If you want to apply your rule on a specific domain, you can add this condition
RewriteCond %{HTTP_HOST} ^test\.aroa\.com$ [NC]
Also, a small semantic detail: you don't need L flag with P flag (it's redundant, since P includes L)
Finally, your code should look like this
RewriteCond %{HTTP_HOST} ^test\.aroa\.com$ [NC]
# your other conditions
RewriteRule ^/?(.+)$ /xyz/abc/xy/$1.html [P]
Remark: i think you don't need to use mod_proxy (P flag) especially because it's slower than a simple internal rewrite. If your (sub-)domains share the same document root then you can avoid using it by replacing P flag with L flag

.htaccess help needed to make nice looking referral link

i need help regarding nice looking referral link. for example here is a referral link
http://www.my-domain.com/register.php?ref=john.doe
this is a perfect url but not looks good like the following
http://www.my-domain.com/john.doe
how can i achieve this using .htaccess file? please note that, i have index.php, member.php and other many php files in my server. moreover, if someone write my-domain.com it need to hit index.php file.
any help is highly appreciated.
Based on your example you can use the following .htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^([a-zA-Z]+\.php)$ $1 [NC,L]
RewriteRule ^([a-zA-Z]+\.[a-zA-Z]+)$ register.php?ref=$1
This works as following:
Line 1: Sets your index file to index.php if someone accesses http://www.my-domain.com/. This should already be working, but just in case.
Line 2: Enables the RewriteEngine.
Line 3: If someone wants to access anything (technically: anything with at least one character) ending with .php, it is just forwarded (i.e. foo.php will be mapped to foo.php). [NC,L] enables case insensitive matching (for the extension - you never know) and prevents any further rules from being executed. Otherwise the second one would also match every time.
Line 4: If someone wants to access anything matching "at least one character, a dot, at least one more character", then this will be mapped to register.php?ref=<input>
Note: This will effectively prevent all user names ending with .php, but allow access to all your files. It will also prevent user names containing less or more than one dot and it will in its current form not work if your files or user names contain any other characters (e.g. foo_bar.php or i_love_php). But those two limitations can be easily overcome if needed, just provide more details regarding expected behaviour.
You could add a RewriteCond to check if there actually exists a .php file with the requested name and treat it as user name otherwise, but I really don't think you should do that (think about adding new files).
This will get you what you need. This rule will handle nice link and also redirect old link to nice link.
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /+register\.php\?ref=(.+)
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /register.php?ref=$1 [L]

Why are my .htaccess rules not working?

I have a cascade problem with my .htaccess rules. Consider the following:
RewriteEngine on
RewriteRule ^product/(.*)$ product.php [L,QSA]
RewriteRule ^(.*)$ index.php [L]
With the above, if I requested a URL like http://example.com/product/product-slug, then I’d expect the request to get routed to product.php. However, it doesn’t; my index.php script is picked the request up.
I would have thought that the first RewriteRule would be matched, and as it has a L (last) flag that no further RewriteRules would be matched, including the “catch-all” one at the bottom.
Why is this not working as expected?
This should sort it:
RewriteRule ^product/(.*)$ product.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !product.php
RewriteRule ^.*$ index.php [L]
The problem is that because the rules were in different sets, i.e. not attached a condition, it only stopped processing the current set of rules (the first one) and jumped onto the second.
Hope that clears it all up :)
Perhaps a typo in your code? You're writing "http://example.com/products/" in your question, but in the code you're targeting ^product$, with no s.
Also, your first rule is too strict. It will only match http://example.com/product/. You need to include a wild card after product to allow it to pick up product-slug. Something like RewriteRule ^products/(.*)$ product.php [L,QSA] should work.
Is it not because of the order you have placed the rules in? The one below will override changes to the one above it. Try changing them around.
Also, do you need to set the RewriteBase or not? Is your project on an actual domain, or locally stored in a sub-directory of the server root?

mod rewrite rule for parameters

I have below url(s)
www.localhost.com/profile.php?username=first.last
i would like to permanently redirect above url to using .htaaccess file. (apache server)
www.localhost.com/first.last
please also consider there are few other urls there but i dont want to touch them..like
www.localhost.com/message.php?id=12
www.localhost.com/editprofile.php?editname=first.last
www.localhost.com/uploadphoto.php?username=first.last
can anyone please help me.
thank you in advance.
You could try to handle the Query String with RewriteCond and pass the captured match to RewriteRule. You must exclude any .phpscripts of your rewriting rule otherwise it will create some problems with others URLs.
Don't forget to add the [QSA] tag after your RewriteRule otherwise it will not add the Query String parameters.
Maybe doing something like this:
RewriteEngine on
#serve any existing php scripts as usual
RewriteRule ^([a-zA-Z0-9]+\.php) - [L]
#and now handle your specific stuff:
RewriteCond %{QUERY_STRING} ^([a-zA-Z0-9]+\./[a-zA-Z0-9]+)$
RewriteRule ^(%1)$ /profile.php?username=%1 [QSA]
I don't test it but it should be a good beginning. You can read some good stuff here and inside the docs for mod_rewrite httpd 2.2 about how to write and handle specific rewriting use cases.

Apache Mod rewrite help

I've been working on a solution to this for several hours now & figured if someone doesn't mind helping me out, it might save me some time. My question is with regards to Apache mod_rewrite; of course there is tons of documentation out there, however nothing specific to my requirements which are:
to take a URL in this format:
language/pagename.php
(language will either be 'english' or 'french', I will write a separate rule for each. [only need an example for one though]. page name will be any word character (w+). all URLs will have a .php extension).
And then rewrite it so the URL doesn't change in the users browser, but so that php could receive it in this format:
language/page.php?slug=pagename
e.g. so $_GET['slug'] would return the value pagename, and all requests are then handled by page.php.
So far my best guess is
RewriteEngine On
RewriteBase /
RewriteRule ^english/(\w+).php$ english/page.php?slug=$1
However this make php tell me that slug=page for this URL for example english/financial.php; rather than financial.
Have tried a bunch of other regex conventions too (.) instead of w & so on..
Use these rules:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(english|french)/([^/\.]+)\.php$ /$1/page.php?slug=$2 [NC,QSA,L]
This needs to be place in .htaccess file in root folder. If you will be placing it config file (e.g. httpd-vhost.conf, inside <VirtualHost> directive), then rule needs to be slightly altered.
This rule should work for any language, as long as you add it into the rule (english|french part).
This rule has a condition which will not rewrite if such file already exists. This should solve your problem with slug=page: in your rule you most likely have a rewrite loop (after rewrite occurs it goes to the next iteration -- that's how mod_rewrite works, and you need to have some logic in place to break this loop). Instead of RewriteCond %{REQUEST_FILENAME} !-f you could use RewriteCond %{REQUEST_URI} !^/(english|french)/page\.php [NC] but it is a bit more difficult to maintain (you need to add languages here as well as in rewrite rule itself).
If you already have some other rewrite rules then take care with placing these in correct place (order of rules matters).
Because I do not know for sure what page names (slugs) would be, I've used this pattern: [^/\.]+ (any characters except / or .) .. but you may change it to \w+ or whatever you think will be better.
Rule preserve any optional page parameters (query string) -- useful for preserving referrals/tracking code etc.

Resources