htaccess external rewrite / internal redirect - regex

There are two things that I would like to achieve with .htaccess file.
The first one is
www.hostname.com/index.php?question -> www.hostname.com/question
www.hostname.com/index.php?myinfo -> www.hostname.com/myinfo
www.hostname.com/index.php?notification -> www.hostname.com/notification
so I use external rewrite to re-express on the URL as following.
RewriteCond %{THE_REQUEST} /(index.php)\?([^&]+)\ HTTP
RewriteRule ^ /%2? [R=301,L]
Now the above statement correctly displays as I want. The problem is to internally convert back when a condition is satisfied. The condition is if %{THE_REQUEST} is equal to any character after '/'.
RewriteCond %{REQUEST_URI} ^/(.*)$ [NC]
RewriteRule ^(.*)$ index.php?$1 [NC,L]
So that my php code can recognize the $_GET parameter. Here, even though the condition is satisfied it will not process the RewriteRule.
The second problem is
www.hostname.com/index.php?category=spo -> www.hostname.com/category/spo
www.hostname.com/index.php?category=ite -> www.hostname.com/category/ite
www.hostname.com/index.php?category=gam -> www.hostname.com/category/gam
The conversion is completed using exteral rewrite:
RewriteCond %{THE_REQUEST} /(index)\?category=([^&]+)\ HTTP
RewriteRule ^ category/%2? [R=301,L]
Again, I'd like to convert back whatever is written in the URL back to the original format internally so I use the following condition to differentiate from the previous case,
RewriteCond %{REQUEST_URI} ^/category/(.*)$
RewriteRule ^category/(.*)/?$ index.php?category=$1 [NC,L]
my php code cannot recognize the $_GET parameter and variable. When I use htacces tester, it says it should work but it doesn't. http://martinmelin.se/rewrite-rule-tester/
How do I fix this problem? OR is there any easier way to accomplish this?
Thank you

Try these rules:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/index\.php\?category=([^&\s]+) [NC]
RewriteRule ^ /category/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^/]+)/?$ /index.php?category=$1 [NC,L,QSA]
RewriteCond %{THE_REQUEST} \s/index\.php\?([^&\s]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?$1 [QSA,L]

Related

.htaccess RewriteRule with two different conditions

I have two different URLs:
1.: mysite.com/file.php
2.: mysite.com/**articles**.php?article=**something**
I would like to rewrite mysite.com/file.php to mysite.com/file.
AND
I would like to rewrite mysite.com/**articles**.php?article=something to mysite.com/**news**/**something**
I tried this but it's not working.
It rewrite mysite.com/file.php to mysite.com/file, but does not rewrite mysite.com/**articles**.php?article=**something** to mysite.com/**news**/**something**
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?\ ]+)\.php
RewriteCond %{REQUEST_FILENAME} !exception.php
RewriteRule ^news/([^/]+)(/*)$ /articles.php?article=$1
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
(There is one exception in the code: exception.php which I don't want to rewrite.)
Can you please correct my code? I've been working on this for 4 days, I've read everything, but I can't do it.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect for file.php file.
RewriteCond %{REQUEST_URI} !/?exception\.php [NC]
RewriteCond %{THE_REQUEST} \s/(file)\.php/?\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite for php files.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ $1.php [QSA,L]
##external + internal rewrite rules for articile.php here.
RewriteCond %{THE_REQUEST} \s/articles\.php\?article=(\S+)\s [NC]
RewriteRule ^ /news/%1? [R=301,L]
RewriteRule ^news/([^/]*)$ articles.php?article=$1 [NC,QSA,L]

How do I rewrite a URL with multiple variable "directories" in different possible constellations?

So, I want to get the following rewrites to happen:
example.com/dests/?refs -> example.com/redir.php?dest=dests&ref=refs
example.com/?refs/dests -> example.com/redir.php?dest=dests&ref=refs
example.com/?refs -> example.com?ref=refs
example.com/dests -> example.com/redir.php?dest=dests
Currently I have the following:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^?].*)/$ /?ref=$1 [S=3]
RewriteRule ^/[?](.*)/$ /redir.php?dest=$1 [S=2]
RewriteRule ^/([^?].*)/[?](.*)/$ /redir.php?dest=$1&ref=$2 [S=1]
RewriteRule ^/[?](.*)/([^?].*)/$ /redir.php?dest=$2&ref=$1
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
</IfModule>
with the error being that I get a 404 when calling /dests and the rest doing nothing.
I have checked that the regexps are valid (except for changing / to / b/c PhpStorm told me to, but that didn't work either).
Does anyone have an idea what I'm doing wrong here?
Thanks in advance
Alix aka B00tLoad_
When your redirects are based on ?xy query string params you need to access the RewriteCond %{QUERY_STRING} like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#example.com/?refs -> example.com?ref=refs
RewriteCond %{QUERY_STRING} ^([^=&?/]+)$
RewriteRule ^$ ?ref=%1 [R=301,L,NE]
#example.com/?refs/dests -> example.com/redir.php?dest=dests&ref=refs
RewriteCond %{QUERY_STRING} ^([^=&?/]+)/([^=&?/]+)$
RewriteRule ^$ /redir.php?dest=%2&ref=%1 [R=301,L,NE]
#example.com/dests/?refs -> example.com/redir.php?dest=dests&ref=refs
RewriteCond %{QUERY_STRING} ^([^=&?/]+)$
RewriteRule ^(dests)/$ redir.php?dest=$1&ref=%1 [R=301,L,NE]
#example.com/dests -> example.com/redir.php?dest=dests
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)$ redir.php?dest=$1 [R=301,L,NE]
I've tested the rules, however, you might need to make small adjustments. E.g. you may need to specify ^index\.php$ (or whatever your default document is) instead of ^$.

Redirect to Rewritten URL issue

I 'm having a problem when trying to redirect to rewritten url.
I'm rewrite from :
www.testdomain.com/index.php?rt=..&id=..
to :
www.testdomain.com/{rt}/{id}
and trying to redirect from the old (1) to (2).
Here, my .htaccess file:
RewriteRule ^(.*)/([0-9]+)/?$ index.php?rt=$1&id=$2 [L]
RewriteCond %{REQUEST_URI} ^/index.php
RewriteCond %{QUERY_STRING} ^rt=(.*)&id=(.*)$
RewriteRule ^(.*)$ http://www.localdomain.com/%1/%2? [R=301,L]
And this is my problem: When I'm run in browser, It throw an error about redirect loop. But I don't know how to fix this error.
Replace your code with:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?rt=([^\s&]+)&id=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([0-9]+)/?$ /index.php?rt=$1&id=$2 [L,QSA]

url rewriting in .htaccess

I am trying to serve different urls using mod_rewrite but whatever I try it is just not working.
An example url would be
http://www.site.com/country/tours/dynamic-part/?&city=new-york,los-angeles
And I am trying to change the url using .htaccess to:
http://www.site.com/country/tours/dynamic-part/new-york,los-angeles
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/.+city=([^\/]*)$ http://www.site.com/country/tours/$1/$2 [L,R=301]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Any ideas? I though I was close but not anymore :/
The RewriteRule does NOT match the query string, see
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#what_is_matched
So the .+city part of the rule will never match.
This should work tho...
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/ http://www.site.com/country/tours/$1/%2 [L,R=301]
The subsitution can read back-referenecs to the RewriteCond pattern.

.htaccess mod_rewrite rules conflicting

I have the following rewrite rules:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$
RewriteCond %{REQUEST_URI} !^/(Account|Logout|Password|Tags) [NC]
RewriteRule ^([^/]*)$ /index.php?city=$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/Account [NC]
RewriteRule ^Account /members/account.php [NC,L]
RewriteCond %{REQUEST_URI} ^/Logout [NC]
RewriteRule ^Logout /members/logout.php [NC,L]
RewriteCond %{REQUEST_URI} ^/Password [NC]
RewriteRule ^Password /members/password.php [NC,L]
RewriteCond %{REQUEST_URI} ^/Tags [NC]
RewriteRule ^Tags /members/tags.php [NC,L]
I'm trying to add another condition that so that it loads as
domain.com/$city/$provider/$name
this is the rule:
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /listing.php?city=$1&provider=$2&urlname=$3 [L]
My issue is that this rule conflicts with the rule for my index file which loads as domain.com/$city.
I'd greatly appreciate any and all suggestions.
Thanks!
There's nothing wrong with any of your rules. Its just that the first covers the last in terms of what it will accept. The easiest way to avoid this problem is to move the more restrictive rule above of the more general. I.e. in this case put the Listing rule immediately before the index.php rule.
Addendum
Oops, The ->listing.php followed by the ->indexp.php rules will still loop because rule1 fires on the first pass and then rule2 on the second, since the query string is stripped for purposes of regexp matching in a rule; and "listing.php matches ^([^/]*)$.
You should only match ^([^/]*)$ in the case where the pattern doesn't match an existing file. The way to prevent this is to put a
RewriteCond %{REQUEST_FILENAME} !-f
before the index.php rule.