I have a .htaccess file and a couple of redirect rule.
For example:
RewriteRule ^blog/migrations/?$ / [R=301,L,NE]
RewriteRule ^blog/digital/?$ / [R=301,L,NE]
Basically, all my blog urls have been removed but i found redirects rule for every single blog url to redirect to main page. But i want this to be in one rule, something like this:
RewriteRule ^blog/{anyUrl}/?$ / [R=301,L,NE]
Also i have strange redirects, such as:
RewriteRule ^2/?$ / [R=301,L,NE]
RewriteRule ^3/?$ / [R=301,L,NE]
RewriteRule ^4/?$ / [R=301,L,NE]
RewriteRule ^5/?$ / [R=301,L,NE]
RewriteRule ^6/?$ / [R=301,L,NE]
And i want them to be one rule, for example:
RewriteRule ^{[2-6]}/?$ / [R=301,L,NE]
Can anyone help with those rule, i am not really good at regexps.
//Edited
Also i have a rule that redirects urls with query strings:
RewriteCond %{QUERY_STRING} .
RewriteRule ^(.*)/?$ /$1 [QSD,R=301,L]
But this rule removes all query strings. How can i exclude from the rule specific query strings that starts with utm_?
And how can i get the domain name? I have my website on two different domains. For example:
RewriteRule ^posts/? https://{domain}/somewhere
You can combine all those rules into one rule using regex alternation:
RewriteRule ^(blog/|[2-6]/?$) https://%{HTTP_HOST}/ [R=301,L,NC]
This pattern ^(blog/|[2-6]/?$) will match start position and then either blog/ or [2-6] followed by optional / and end position.
For the other query string rule you can use:
RewriteCond %{QUERY_STRING} .
RewriteCond %{QUERY_STRING} !(^|&)utm_ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [QSD,R=301,L,NE]
RewriteRule ^posts/? https://{domain}/somewhere
You don't need to use https://%{HTTP_HOST} here in redirect rules to make it same domain as in the request, so make it:
RewriteRule ^posts/? https://%{HTTP_HOST}/somewhere [L,R]
Related
I want to combine these two rules, but not sure how
RewriteRule ^([^\.]+)$ $1.html [L]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
when I put both, I get the error "too many redirects"
My goal here is to combine them both,
the first rule is to remove file extensions (ex. html)
the second rule is: make every URL go to https://www.example.com, rather than https://example.com
With the additional information you now gave I would suggest this approach:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,END]
RewriteRule ^/?(.+)\.html$ /$1 [R=301,END]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [END]
In general it is a good diea to start using a R=302 temporary redirection and only change that to a R=301 permanent redirection once everything works as expected. That prevents nasty caching issues ...
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.
I've moved a website to a new domain and I've used RewriteRule to redirect a number of my URLs.
I'm stuck trying to redirect URLs with query strings in them?
RewriteRule /news/archive.php?cat=economic-impact http://www.new-website.com/faqs/economic-impact [R=301,L]
RewriteRule /news/archive.php?cat=housing-need http://www.new-website.com/faqs/housing-need R=301,L]
RewriteRule /news/archive.php?cat=infrastructure http://www.new-website.com/faqs/infrastructure R=301,L]
Further up the htaccess file I'm using this to redirect the domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old-website.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old-website.com$
RewriteRule (.*)$ http://www.new-website.com/$1 [R=301]
When I visit:
old-website.com/news/archive.php?cat=economic-impact
it redirects to
new-website.com/news/archive.php?cat=economic-impact
Which 404s, I need to to redirect to:
new-website.com/faqs/economic-impact
Can anyone help?
You need to add a blank query string on your rewrite to truncate any existing query strings.
The below should do the job:
RewriteRule (.*)$ http://www.new-website.com/$1? [R=301]
In addition, the way you're checking the existing query strings won't work. You'll need to check them separately:
RewriteCond %{QUERY_STRING} "cat=(economic-impact|housing-need|infrasructure)"
RewriteRule news/archive.php http://www.new-website.com/faqs/%1? R=301,L]
The %1 will take a backreference from a regex group match in the RewriteCond
I have to remap a few ID's to URL strings (301 redirect) and I have to do it with mod_rewrite:
/page.php?id=15 to /pagexy
/page.php?id=10 to /pageyz
The rule:
RewriteRule ^page.php?id=15$ /pagexy [L,R=301]
doesn't work. What am I doing wrong?
You need to inspect the query string separately. The following should work:
RewriteEngine On
RewriteCond %{QUERY_STRING} id=15\b [NC]
RewriteRule ^/page.php$ /pagexy? [L,R=301]
RewriteCond %{QUERY_STRING} id=10\b [NC]
RewriteRule ^/page.php$ /pageyz? [L,R=301]
Given you are tying ids to pages, it may also pay to look at using a RewriteMap
I just want to redirect the URL through the mod rewrite ruls. I have applied this rule excluding (R=301)
Example :
from http:///webapp/wcs/stores/servlet/en/marksandspencer to http:///en/marksandspencer
I am using this rules for the mod redirect rules.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^(/)?$
RewriteCond %{REQUEST_URI} !^/webapp.*$
RewriteCond %{REQUEST_URI} !^/wcsstore.*$
RewriteRule ^/(.*)$ /webapp/wcs/stores/servlet/$1 [PT,NC,L,QSA]
RewriteRule ^/webapp/wcs/stores/servlet/(.*) /$1 [NE,L,QSA]
RewriteRule ^(/)?$ /webapp/wcs/stores/servlet/en/marksandspencer [PT,NC,L]
No idea what you're trying to do, but if you're using Apache 2.0 or higher, the leading slash is stripped off of URI's when matching is done within a RewriteRule. Also, you have a rule that looks like you're adding a /webapp/wcs/stores/servlet/ to the beginning of a URI, then the very next rule it looks like you are removing it. This will probably cause a loop.
Taking a wild guess at what you are trying to do, I think you need to add a condition to the 2nd rule, and remove the leading slashes:
# internally rewrite URI by appending "/webapp/wcs/stores/servlet/" to the front
RewriteCond %{REQUEST_URI} !^(/)?$
RewriteCond %{REQUEST_URI} !^/webapp.*$
RewriteCond %{REQUEST_URI} !^/wcsstore.*$
RewriteRule ^(.*)$ /webapp/wcs/stores/servlet/$1 [PT,NC,L,QSA]
# if a request is made with "/webapp/wcs/stores/servlet/" in it, redirect to a URI with it stripped
RewriteCond %{THE_REQUEST} ^(GET/POST)\ /webapp/wcs/stores/servlet/
RewriteRule ^webapp/wcs/stores/servlet/(.*) /$1 [R=301,L,QSA]
RewriteRule ^$ /webapp/wcs/stores/servlet/en/marksandspencer [PT,NC,L]