I have a website example.com and I am passing two GET parameters in the url.
example.com/page.php?page=5§ion=10
Now I want it to show
example.com/5/10
I've already got it to work for the first part but cannot seem to figure out the second part (section part)
My current .htaccess file is
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+page\.php\?page=(\d+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteRule ^(\d+)/?$ page.php?page=$1 [L,QSA,NC]
All I need to get is the second part working (§ion=10)
You can use this
RewriteEngine on
#redirect and rewrite the single get perm /?page
RewriteCond %{THE_REQUEST} \s/+page\.php\?page=(\d+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteRule ^(\d+)/?$ page.php?page=$1 [L,QSA,NC]
#redirect and rewrite urls with multiple perms
RewriteCond %{THE_REQUEST} \s/+page\.php\?page=(\d+)§ion=(\d+) [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]
RewriteRule ^(\d+)/(\d+)/?$ page.php?page=$1§ion=$2 [L,QSA,NC]
Add this to your .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} page=(.+)§ion=(.+)$ [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]
This grabs the variables using {QUERY_STRING} and rewrites your URL to http://example.com/5/10. The use of the ? on the end of the rewrite is to stop the query from appending onto the end after the rewrite.
Make sure you clear your cache before testing this.
Related
I've got the following in my httpaccess file on my apache http server.
RewriteEngine on
RewriteCond %{THE_REQUEST} /web/([^\s]+) [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteRule ^((?!web).*)$ /web/$1 [NC,L]
and it's working properly. But i wan to apply this condition for all urls except when I ask for /correoimages/ then I want that the url be the same url that is requested. How can I do that?
That is when I ask for https://example.org/CORREOIMAGES/a.jpg the url must be https:///example.org/correoimages/a.jpg and not https://example.org/web/correoimages/a.jpg. In the rest of the case web must be added.
I dont´understand properly RewriteRule and RewriteCond.
You can simply add an exception to your rule set:
RewriteEngine on
RewriteCond %{THE_REQUEST} /web/([^\s]+) [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteRule ^correoimages/ - [L]
RewriteRule ^((?!web).*)$ /web/$1 [NC,L]
I want to redirect all request of one particular domain to another, except for one page request. I hope my attempt explains what I try to do:
RewriteEngine on
Rewritecond %{HTTP_HOST} !^host\.mysite\.com
RewriteRule ^link/([^/]+)/([^/]+)/([^/]+)$ dokument_by_link.php?$1=1&document_type=$2&value=$3 [NC,L]
Rewritecond %{HTTP_HOST} !^host\.mysite\.com
RewriteCond %{REQUEST_URI} !^/link/
RewriteCond %{REQUEST_URI} !^dokument_by_link\.php
RewriteRule (.*) https://www.anothersite.de/$1 [R=302,L]
#This should not apply to host.mysite.com, but I think this is already accomplished by the L-flag above
RewriteRule ^test/?$ test.php [L,NC]
host.mysite.com/link/ should be redirected to host.mysite.com/document_by_link.php
RewriteRule ^link/([^/]+)/([^/]+)/([^/]+)$ dokument_by_link.php?$1=1&document_type=$2&value=$3 [NC,L]
All other requests should be redirected to https://www.anothersite.de
Thank you very much!
This should do the trick :
RewriteEngine on
#rewrite /link to /document_by_link.php
RewriteRule ^link/?$ /document_by_link.php [L]
#redirect all other URLs to https://www.anothersite.de
RewriteRule !dokument_by_link\.php$ https://www.anothersite.de%{REQUEST_URI} [L,R]
This should work for you:
RewriteEngine On
Rewritecond %{HTTP_HOST} ^host\.mysite\.com$ [NC]
RewriteCond %{THE_REQUEST} !\s/link/ [NC]
RewriteRule ^ https://www.anothersite.de%{REQUEST_URI} [R=302,L,NE]
Rewritecond %{HTTP_HOST} ^host\.mysite\.com$ [NC]
RewriteRule ^link/([^/]+)/([^/]+)/([^/]+)$ dokument_by_link.php?$1=1&document_type=$2&value=$3 [NC,L,QSA]
RewriteRule ^test/?$ test.php [L,NC]
Using THE_REQUEST instead of REQUEST_URI here as REQUEST_URI may change to a rewritten URI whereas THE_REQUEST remains same for the scope of a web request.
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1
I'm trying to rewrite a query string to a path, like so:
http://example.com/?p=page1
to
http://example.com/page/page1
The internal redirect works and I can view the page at the second URL but as soon as I try to redirect the first URL to the second, I get a 'Too many redirects' error.
.htaccess:
RewriteCond %{REQUEST_URI} /page/(.*)
RewriteRule page/(.*)$ index.php?p=$1 [L,QSA,NC]
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule (.*) /page/%1? [R=301,L,NE,QSD,NC]
The first two lines are working by themeselves. The addition of the last two lines causes the error.
Your rule is redirecting the uri back it itself that is why you got the redirect error.
You can use %{THE_REQUEST} or %{ENV_REDIRECT_STATUS} variables to avoid Too many redirect error .
RewriteCond %{REQUEST_URI} /page/(.*)
RewriteRule page/(.*)$ index.php?p=$1 [L,QSA,NC]
RewriteCond %{ENV_REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule (.*) /page/%1? [R=301,L,NE,QSD,NC]
or
RewriteCond %{REQUEST_URI} /page/(.*)
RewriteRule page/(.*)$ index.php?p=$1 [L,QSA,NC]
RewriteCond %{THE_REQUEST} /\?p=.+ [NC]
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule (.*) /page/%1? [R=301,L,NE]
How can I redirect both these URL's back to /blog/ and covering any variations that also include ?page_id for the first one and no_redirect for the second one.
https://www.example.com/blog/page/10/?page_id=%2Ffeed%2Fatom%2F
and
https://www.example.com/blog/page/10/?no_redirect=true
Appreciate the help!
Try :
RewriteEngine on
RewriteCond %{THE_REQUEST} /blog/page/10/\?page_id=%2Ffeed%2Fatom%2F [NC,OR]
RewriteCond %{THE_REQUEST} /blog/page/10/\?no_redirect=true [NC]
RewriteRule ^ /blog/? [L,R]
EDIT :
here is an another way to redirect everything after the /blog/page/* to /blog/
RewriteEngine on
RewriteCond %{THE_REQUEST} /blog/page/ [NC]
RewriteRule ^ /blog/? [L,R]
I want to have /path/short serve up the content held by the file /path/short.html, but if the user types /path/short.html or /path/short/ I want him to be redirected (with 301) to /path/short. This is what I have so far in my .htaccess:
RewriteEngine On
RewriteBase /path
RewriteCond %{REQUEST_URI} !=/path/handle.html
RewriteCond %{REQUEST_URI} !^/path/short(.html|/)?$
RewriteRule ^(.+) /cgi-bin/handle\.cgi?$1 [PT,QSA]
RewriteRule ^path$ /path/short.html
#-------
RewriteRule ^short/$ /path/short [R=301,L]
# RewriteRule ^short.html$ /path/short [R=301,L]
The rules reflect the fact that handle.cgi will receive all other requests (ie, /path/this, /path/, /path/somethingelse, etc.) which is working great. So far, the /path/short/ redirects correctly to /path/short and /path/short is properly showing the content in /path/short.html, however, uncommenting the last line causes a loop. So, how do I get this to work?
You can use this code in /path/.htaccess:
RewriteEngine On
RewriteBase /path/
RewriteCond %{THE_REQUEST} /(short)(/|\.html) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteRule ^(short)/?$ $1.html [L,NC]
A friend pointed me towards another question which helped me arrive at this solution:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/ads/short(.html|/)?$
RewriteRule ^(.+) /cgi-bin/handle\.cgi?$1 [PT,QSA]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^short(.html|/)$ /ads/short [R=301]
RewriteRule ^short$ /ads/short.html
which works perfectly.