I am attempting to get the following results with an .htaccess file.
URL: Behavior(rewrite to):
example.com/x/ view-test.php?page=x
example.com/x/y view-test.php?page=y&parent=x
example.com/x/y/z view-test.php?page=z&parent=y&grandparent=z
I have the following .htaccess rules, which also include some folders which needs to ignore this scheme:
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]*)/([^/]+)/?$ /view-test.php?grandparent=$1&parent=$2&page=$3 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]+)/?$ /view-test.php?&parent=$1&page=$2 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
#RewriteRule ([^/]+)$ /view-test.php?page=$1 [L]
however, whenever I uncomment the last two lines I get an internal server error. the parent/page and grandparent/parent/page structures work fine. How can I re-write this last rule (and the preceding rules if nessecary) to achieve the desired result for the first pattern (just a page)?
# remove slash at the end to prevent redirect loop
RewriteCond %{REQUEST_URI} ^/(OIE|admin|images|scripts|css|fonts|view-test\.php) [NC]
RewriteRule ^ - [L]
RewriteRule ([^/]*)/([^/]*)/([^/]+)/?$ /OIE/view-test.php?grandparent=$1&parent=$2&page=$3 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]+)/?$ /OIE/view-test.php?&parent=$1&page=$2 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]+)/?$ /OIE/view-test.php?page=$1 [L]
Use this code instead:
RewriteCond %{REQUEST_URI} ^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ^ - [L,S=3]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ view-test.php?grandparent=$1&parent=$2&page=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ view-test.php?&parent=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ view-test.php?page=$1 [L,QSA]
Problem was that you were missing trailing slash / in in your last RewriteRule condition.
Try this :
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ view-test.php?page=$1
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ view-test.php?page=$1&parent=$2
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ view-test.php?page=$1&parent=$2&grandparent=$3
Related
I am facing a couple of 301 redirect issues.
Issue 1:
I want to redirect some links to another URL of the same site:
Redirect 301 /path-example.html /dir/subdir/
The redirect is working but it's generating query string like this:
example.com/dir/subdir/?str=path-example
How do I remove the query sting ?str=path-example ?
Issue 2:
I want to redirect some links to the homepage:
Redirect 301 /some-path.html /
But it's leaving a ? after a trailing slash like this:
example.com/?
How do I remove /? from the forwarded URL?
Existing rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{QUERY_STRING} ^s=(.*)$
RewriteRule ^$ / [R=301,L]
Thanks!
Please place these rules top of your htaccess files(in case you have more rules in it). Please make sure to clear your browser cache before testing your URLs.
<IfModule mod_rewrite.c>
RewriteEngine ON
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^index\.php/?$ - [L,NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
##Redirection with removing query string rule here.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\?\S+\s [NC]
RewriteRule ^ /dir/subdir? [R=301,L]
##Redirection to site's home page here for html urls.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\s [NC]
RewriteRule ^ /? [QSD,R=301,L]
RewriteCond %{QUERY_STRING} ^s [NC]
RewriteRule ^/?$ / [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
EDIT: My first answer without OP's other rules set is as follows:
RewriteEngine ON
##Redirection with removing query string rule here.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\?\S+\s [NC]
RewriteRule ^ /dir/subdir? [R=301,L]
##Redirection to site's home page here for html urls.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\s [NC]
RewriteRule ^ /? [QSD,R=301,L]
For a website I have locally, I need to set the display language as if it were a subfolder, using an htaccess file.
For example, if I choose to display the site in German, instead of displaying it like this => local_ip/test/index.php?lang=de, I would like it to be displayed like this => local_ip/test/de/ .
Also, if I type in the url without setting the language, I would like htaccess to automatically redirect to the German language, from so => local_ip/test/tour/mountain/ to so => local_ip/test/de/tour/mountain .
My current htaccess file is structured as follows:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{QUERY_STRING} ^t=(.+?)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&t=%1 [NC,L]
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&p=%1 [NC,L]
RewriteCond %{QUERY_STRING} ^t=(.+?)&p=([0-9]+)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&t=%1&p=%2 [NC,L]
RewriteCond %{QUERY_STRING} ^c=true
RewriteRule ^city/(.+?)/?$ city.php?url=$1&c=true [NC,L]
RewriteRule ^city/(.+?)$ city.php?url=$1 [NC,L]
RewriteRule ^tour/(.+?)$ tour.php?url=$1 [NC,L]
SOLVED
After several attempts and after reading several support requests posted in this community, I found a way to make the system recognise all the other rules as well. All I had to do was put them before the last rule provided by the user who supported me. Here is the final code.
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !/de/ [NC]
RewriteRule (.*) /test/de/$1 [R,L]
RewriteRule ^de/$ /test/index.php?lang=de [L]
RewriteCond %{QUERY_STRING} ^t=(.+?)
RewriteRule city/(.+?)/?$ city.php?url=$1&t=%1&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule city/(.+?)/?$ city.php?url=$1&p=%1&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^t=(.+?)&p=([0-9]+)
RewriteRule city/(.+?)/?$ city.php?url=$1&t=%1&p=%2&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^c=true
RewriteRule city/(.+?)/?$ city.php?url=$1&c=true&lang=de [NC,L]
RewriteRule city/(.+?)$ city.php?url=$1&lang=de [NC,L]
RewriteRule tour/(.+?)$ tour.php?url=$1&lang=de [NC,L]
RewriteRule ^de/(.*)$ /test/$1 [L]
You can use the following :
RewriteEngine on
#redirect URLs to include /de
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/de/ [NC]
RewriteRule (.*) /test/de/$1 [R,L]
# /test/index.php?lang=de to /test/de
RewriteRule ^de/$ /test/index.php?lang=de [END]
RewriteRule ^de/(.+)$ /test/$1 [END]
I provided this .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.lebmotors.com/new/
RewriteRule (.*) http://www.lebmotors.com/new/$1 [R=301,L]
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ moreprod.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ moreprod.php?id=$1
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ moreprod.php?id=$1&topid=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ moreprod.php?id=$1&topid=$2
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ moreprod.php?id=$1&topid=$2&catid=$3
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ moreprod.php?id=$1&topid=$2&catid=$3
Still it does not work
I get: http://www.lebmotors.com/new/moreprod.php?id=107&topid=4&catid=8
What AM I doing wrong
Many Thanks
First of all, you don't need to repeat RewriteEngine On directive.
Then, when you add your rules, you still can access your pages via moreprod.php (but you can also access it from new clean url).
Here's how your htaccess should look like, assuming moreprod.php is also in new folder
Options -MultiViews
RewriteEngine On
RewriteBase /new/
RewriteCond %{THE_REQUEST} /moreprod\.php\?id=([^&\s]+)\s [NC]
RewriteRule . moreprod/%1/? [R=301,L]
RewriteCond %{THE_REQUEST} /moreprod\.php\?id=([^&\s]+)&topid=([^&\s]+)\s [NC]
RewriteRule . moreprod/%1/%2/? [R=301,L]
RewriteCond %{THE_REQUEST} /moreprod\.php\?id=([^&\s]+)&topid=([^&\s]+)&catid=([^&\s]+)\s [NC]
RewriteRule . moreprod/%1/%2/%3/? [R=301,L]
RewriteCond %{THE_REQUEST} /morenews\.php\?id=([^&\s]+)\s [NC]
RewriteRule . morenews/%1/? [R=301,L]
RewriteRule ^morenews/([^/]+)/$ morenews.php?id=$1 [L]
RewriteRule ^moreprod/([^/]+)/$ moreprod.php?id=$1 [L]
RewriteRule ^moreprod/([^/]+)/([^/]+)/$ moreprod.php?id=$1&topid=$2 [L]
RewriteRule ^moreprod/([^/]+)/([^/]+)/([^/]+)/$ moreprod.php?id=$1&topid=$2&catid=$3 [L]
With this code, if you try (for example) to reach http://domain.com/new/moreprod.php?id=123&topid=456&catid=789 then you'll redirect to http://domain.com/new/moreprod/123/456/789/
Conclusion
You can now access your pages via clean urls like
http://domain.com/new/moreprod/123/ (id=123)
http://domain.com/new/moreprod/123/456/ (id=123 and topid=456)
http://domain.com/new/moreprod/123/456/789/ (id=123 and topid=456 and catid=789)
Resolved, just moved these lines to the bottom of .htaccess
RewriteEngine On
RewriteBase /new/
....
RewriteCond %{THE_REQUEST} /moreprod\.php\?id=([^&\s]+)\s [NC]
RewriteRule . moreprod/%1/? [R=301,L]
.....
RewriteCond %{THE_REQUEST} /moreprod\.php\?id=([^&\s]+)&topid=([^&\s]+)&catid=([^&\s]+)&cagid=([^&\s]+)\s [NC]
RewriteRule . moreprod/%1/%2/%3/%4/? [R=301,L]
RewriteRule ^moreprod/([^/]+)/$ moreprod.php?id=$1 [L]
.....
RewriteRule ^moreprod/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ moreprod.php?id=$1&topid=$2&catid=$3&cagid=$4 [L]
# THIS TO THE BOTTOM
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
I have this .htaccess:
RewriteEngine On
RewriteRule ^!/(.*)$ path/to/a/file/$1 [L]
RewriteRule ^(.*)$ path/to/another/file/$1 [L]
I want urls in the form of www.website.com/!/this/ to be rewritten to path/to/a/file. Any URL that doesn't match that pattern should be rewritten to path/to/another/file/.
Here's what I've tried to far:
RewriteEngine On
RewriteRule ^!/(.*)$ path/to/a/file/$1 [L]
RewriteCond ...
RewriteRule ^(.*)$ path/to/another/file/$1 [L]
When using the above rewrite rule, I get a 500 - Internal Error.
How can I fix it?
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/!
RewriteRule ^!/([a-z0-9_\-\.]+) user/public/$1 [L]
RewriteCond %{REQUEST_URI} !^/!
RewriteRule ^([a-z0-9_\-\.]+)/([a-z0-9_\-\.]+)/?$ $1/controller/front.php/$2 [L]
RewriteCond %{REQUEST_URI} !^/!
RewriteRule ^([a-z0-9_\-]+)/([a-z0-9_\-]+)/([a-z0-9_\-]+)/?$ $1/controller/front.php/$2/$3 [L]
I have pagination plugin in my CMS which produces ?pg=1 links. I want to redirect this url without this ugly postfix because without it CMS shows first page too.
So, i have url like http://site.ru/category/schock-crane/?pg=1 or http://site.ru/moyka/?type=granit&pg=1
I want to redirect such urls to http://site.ru/category/schock-crane/ and http://site.ru/moyka/?type=granit respectly.
I tried this .htaccess code
RewriteRule (.*)(&|\?)pg=1$ $1 [R=301,L]
I tried this regexp code at regexp trainers - it worked. But at live server no redirect happens.
Here is whole .htaccess file:
AddDefaultCharset Off
#DirectoryIndex index.php index.html
Options +FollowSymLinks
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# fix /?pg=1
RewriteRule (.*)(&|\?)pg=1$ $1 [R=301,L]
# fix .palitra-tab
RewriteCond %{REQUEST_URI} -tab$
RewriteRule (.*)/([0-9]*)/\.(.*)-tab?$ http://site.ru/redirect.php?id=$2 [R=301,L]
RewriteCond %{REQUEST_URI} ^/csss/.* [NC]
RewriteRule csss/(.*)\.css$ css.php?n=$1 [L]
#RewriteRule ^(.*)/csss/(.*) /test.php [L]
RewriteRule ^(.*)/base([0-9]+)/?$ $1?base=$2
#RewriteCond %{REQUEST_FILENAME} -f
#RewriteRule \.(gif|jpeg|jpg|png)$ /images/watermark/index.php [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*) index.php
RewriteCond %{HTTP:Authorization} !^$
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
</IfModule>
You need to use RewriteCond to examine the query string:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)pg=1(&(.*)|$)
RewriteCond %1%4 (.*?)&?$
RewriteRule ^ %{REQUEST_URI}?%1 [R=301,L]
The second RewriteCond is just to remove a trailing &.
Maybe the missing ^ or / kills the redirect
# fix /?pg=1
RewriteRule ^(.*)(&|\?)pg=1$ /$1 [R=301,L]
or try
RewriteRule ^(.*)(&|\?)pg=1$ http://%{HTTP_HOST}/$1 [R=301,L]