I'm not sure if it is possible but I want rewrite old URLs to new style and if someone will access the old URL i want to 301 redirect him to new URL style
OLD URL: /catalogue/category/some-category-page
NEW URL: /some-category-page/category
Also what I need is to pass REQUEST_URI to PHP script
I've got something like this but its not working (recursion):
RewriteEngine On
RewriteRule ^catalogue/category/(.+)$ /$1/category [R=301,L] # this is bad I know
RewriteRule ^(.+)/category?$ /catalogue/category/$1 [P]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(pdf|js|ico|gif|jpg|png|css|rar|zip|tar\.gz)$ index.php?modrewrited=%{REQUEST_URI} [QSA,L]
Thank you for help.
If your PHP script needs to read from $_SERVER['REQUEST_URI'], the you need to make sure mod_proxy is loaded. Except that causes a redirect loop. You need to have your first rule which redirects check the actual request variable. So instead of:
RewriteRule ^catalogue/category/(.+)$ /$1/category [R=301,L] # this is bad I know
you want:
RewriteCond %{THE_REQUEST} \ /+catalogue/category/([^\ \?]+)
RewriteRule ^ /%1/category [L,R=301]
Related
Need some help trying to redirect a domain:
https://www.example.com/blog/page/10/?page_id=%2Ffeed%2Fatom%2F to /blog/
I think the best way is to just redirect anything after /page
The code below is what I'm thinking but I know is not correct:
RewriteEngine on
RewriteCond %{THE_REQUEST} /blog/page/[0-255]* [NC]
RewriteRule ^ /blog/? [NC,R=301,L]
Any advice?
Need some help trying to redirect a domain:
If you are trying to redirect the domain use something like
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR]
RewriteRule ^(.*)$ "http://www\.exampe2\.com\/blog/" [R=301,L]
If you want to append the request to the URL use $1
If you're rewriting the path to /blog/ and want to keep the query string use the [QSA] flag ie.
RewriteRule /blog/(.*) /blog/ [R=301,QSA,L]
this will make sure you keep your query string.
I would like to remove part of the url of my Joomla site and redirect to another url for example
www.example.com/xxx/yyy/zzz/FAQ
www.example.com/123/456/FAQ
to
www.example.com/FAQ
I'm very new to mod_rewrite in .htacesss
what RewriteRule can I write to achive this
thank you
Try this in root/htaccess
RewriteEngine On
RewriteCond %{THE_REQUEST} /xx/yy/zz/([^\s]+) [NC]
RewriteRule ^ /%1? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /xx/yy/zz/$1 [NC,L]
And the same rule can be used to rewrite /123/456/faq to /123/456/FAq , just change the rewrite target and the pattern in first Rewritecond.
$1 is part of the regex in rewrite rule, its the part matched between (.+) ,it contains uri.
Thank you, after I tried I finally got it working
this is my solution
RewriteEngine On
RewriteRule ^.*/FAQ$ /FAQ [L,R=301]
by doing this I remove everything between the URL and FAQ
www.abc.com/SOMETHING/SOMETHING2/FAQ
and redirect the URL to
www.abc.com/FAQ
I also found a nice tool to test the mod_rewrite code
http://htaccess.madewithlove.be/
I am a new bee and I want to redirect following URL
m.site.com/subfolder/index.php?user=xxx
to a search engine frendly url like below using .htacess mode rewrite
m.site.com/subfolder/xxx
please explain me the correct way
You can use in the .htaccess file, in the subfolder (It is important):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{QUERY_STRING} ^user=(.+)$
RewriteRule ^ %1? [R=302,L]
RewriteRule ^(.*)$ index.php?user=$1 [L]
Change R=302 for R=301 when test work well.
I think I have read everything I can about htaccess rewrites and I still can't make heads or tails of whats going on. I re made a website for work and all is well except the last designer did some crazy php stuff and all the urls he used have ?=p(pagename) I want to rewite those to (pagename).php then redirect them to with a 301 I am able to get the 301 redirects works just can't figure out how to rewrite the ?p=(pagename) to (pagename).php
You want to be matching against the actual requests, then internally rewrite it back to the query string:
RewriteEngine On
# 301 redirect to php file
RewriteCond %{THE_REQUEST} \ /\?p=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /%1.php?%2 [L,R=301]
# internally rewrite to the query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php$ /?p=$1 [L,QSA]
You need to check the QUERY_STRING and then apply the rewrite rule
RewriteCond %{QUERY_STRING} ^p=(.*)$
RewriteRule ^(.*)$ http://mydomain.com/%1.php [R=301,L]
the ^p=(.*)$ checks for a query string that only has the one variable p=pagename, you will have to modify it if there will be any other variables in the query string it like p=pagename&id=15 etc
I would like to rewrite the English names of php files to their Dutch equivalents.
For example: someurl.com/news.php?readmore=4#comments should become someurl.com/nieuws.php?leesmeer=4#kommentaar. The code from news.php should be executed but nieuws.php should be in the url the arguments should function as well.
I tried several htaccess examples but I can't get it to work.
Any help would be appreciated.
Edit: Working progress from answers below and final solution.
RewriteCond %{QUERY_STRING} ^readmore=(.*)$
RewriteRule ^news.php$ nieuws.php?leesmeer=%1 [R=301,L]
RewriteCond %{QUERY_STRING} !^norewrite[\w\W]*$
RewriteRule ^news.php$ nieuws.php [R=301,L]
RewriteRule ^nieuws.php$ news.php?norewrite [QSA]
RewriteCond %{QUERY_STRING} !^norewrite[\w\W]*$
RewriteRule ^search.php$ zoeken.php [R=301,L]
RewriteRule ^zoeken.php$ search.php?norewrite [QSA]
# make sure rewrite is activ
RewriteEngine On
# Rewrite a request for nieuws.php to news.php
RewriteRule ^nieuws.php$ news.php
Should do the trick.
Instad you could send all requests to an index.php and parse them there:
## Redirect everything to http://hostname/?path=requested/path
RewriteEngine On
RewriteRule ^([\w\W]*)$ index.php?path=$1 [QSA]
[QSA] makes sure you get the original get arguments too.
Now you have to parse the request in $_GET['path'] in you index.php and include the requested page.
eg:
if ($_GET['path'] == 'nieuws.php') {
include 'news.php';
} else if (empty($_GET['path'])) {
echo "HOME";
}
if you want to make make the user always sees nieuws.php in its address bar, even if he requested news.php, you could try the following:
RewriteEngine On
# Redirect news.php to nieuws.php if and only if the request comes from the client
# (suppose the client didn't set ?norewrite.)
RewriteCond %{QUERY_STRING} !^norewrite[\w\W]*$
RewriteRule ^news.php$ nieuws.php [R=301,L]
# Send news.php if nieuws.php was requested and prevent news.php from being redirected
# to back to nieuws.php by the rule above.
RewriteRule ^nieuws.php$ news.php?norewrite [L,QSA]
(R=301 means send a "moved permanently" redirect to the client, L means stop rewriting after this rule matched)
The hole thing with norewrite (you could use something else instead) is only needed to avoid an endles loop of rewriting between news and nieuws.
To translate the GET arguments, you can try the following code before the first line of the above code:
RewriteCond %{QUERY_STRING} ^readmore=(.*)$
RewriteRule ^news.php$ nieuws.php?lesseer=%1 [R=301,L]
Things after a the # in an url can't be changed in .htaccess, since they aren't send to the server at all. The only chance to change them is using JavaScript. (See lots of question here on manipulating them within JavaScript)
Try:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news\.php
RewriteRule ^ /nieuws.php [L,R=301,QSA]
RewriteRule ^nieuws\.php$ /news.php [L,QSA]