Redirect to Rewritten URL issue - regex

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]

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]

htaccess rewrite url name

I've been breaking my head over this one for almost 2 hours now.
I have a website (let's call it example.com)
And I need to rewrite this url:
example.com/plant.php?naam=Brahea%20Armata
To
example.com/Brahae_Armata
This is what my .htaccess file looks like atm:
RewriteEngine On
RewriteCond %{HTTP_HOST} example.com/plant.php?naam=$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
That doesn't work. Anyone know what the problem is?
You can use these rules in root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /plant\.php\?naam=(.*)\%20(\S*)\sHTTP [NC]
RewriteRule ^ /%1_%2? [NE,R=301,L]
RewriteCond %{THE_REQUEST} /plant\.php\?naam=(.*)\sHTTP [NC]
RewriteRule ^ /%1? [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) page.php?name=$1 [QSA,L]

how to redirect with htacess when there are rewrite rules

I've got the following problem:
I need to redirect (301) from /index.php to homepage but there are rewrite rules:
RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|/[^.]*)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php
And there is a loop of redirects when I add line:
Redirect 301 /index.php http://example.com/
How can I do it without changing the rewrite rules? .htaccess is not clear enough for me
Have your rule like this:
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]
RewriteCond %{REQUEST_URI} (\.html?|\.php|/[^.]*)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

Htaccess Rewrite showing 404 not found

I have this rewite rule in my htaccess file that removes index.php on the URL to just show the domain name
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
But it seems to show 404 not found on all other directories. For example, I have a directory called admin and if I go to www.domain.com/admin I get 404 not found
I also have this code above the RewriteCond code
RewriteCond %{REQUEST_URI} !^/admin [NC]
Here is my full htaccess file
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/admin [NC]
RewriteCond %{REQUEST_URI} !^/status [NC]
RewriteCond %{REQUEST_URI} !^/customer [NC]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
# Rewrites /services to be /home.php?id=services
RewriteRule ^([a-zA-Z0-9-]+)/?$ /home.php?id=$1 [L,QSA]
#BLOG
# Rewrites /blog/this-is-a-blog-post to be /home.php?id=blog&slug=this-is-a-blog-post
RewriteRule ^([a-zA-Z0-9-]+)/([\w-]+)/?$ /home.php?id=$1&slug=$2 [L,QSA]
# Rewrites /blog/year2013/month12 to be /home.php?id=blog&year=2013&month=01
RewriteRule ^([a-zA-Z0-9-]+)/year([0-9]+)/month([0-9]+)/?$ /home.php?id=$1&year=$2&month=$3 [L,QSA]
I believe services rules is causing problem here. Change that to:
# Rewrites /services to be /home.php?id=services
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/?$ /home.php?id=$1 [L,QSA]
Do the same for blog rule also:
#BLOG
# Rewrites /blog/this-is-a-blog-post to be /home.php?id=blog&slug=this-is-a-blog-post
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/([\w-]+)/?$ /home.php?id=$1&slug=$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.