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
Related
i need a Solution for the google search console error links and RedirectMatch 301 remove query string / URL Parameters in my htaccess:
Google Search Console Links:
https://www.domain.de/?main_page=index&zenid=umf5etlrmr4blnuiq0e4jsp6l2&cPath=15_326&sort=20a&alpha_filter_id=68
https://www.domain.de/index.php?main_page=product_reviews_write&products_id=9985&cPath=5_380&number_of_uploads=0
https://www.domain.de/?main_page=index&cPath=46_47&sort=20a&alpha_filter_id=70
https://www.domain.de/?currency=USD&main_page=products_new&disp_order=7&page=141
https://www.domain.de/?main_page=index&zenid=mj6nsb9r53goiu6e13nb80tfq7&cPath=1_160&sort=20a&alpha_filter_id=70
https://www.domain.de/?currency=USD&main_page=index&cPath=3_137
https://www.domain.de/?main_page=index&cPath=46_76&sort=20a&alpha_filter_id=84
https://www.domain.de/?main_page=index&cPath=5_6&sort=20a&alpha_filter_id=85
https://www.domain.de/index.php?main_page=index
And many more.
I test this way in the htaccess, but it didn't work:
RedirectMatch 301 ^/?main_page*$ https://www.domain.de/
RedirectMatch 301 ^/?main_page\=$ https://www.domain.de/
RewriteEngine on
RewriteCond %{QUERY_STRING} ^main_page$ [NC]
RewriteRule ^/?main_page?$ https://www.domain.de/ [L,R=301]
You can use the following generic rule to remove query strings
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(main_page|currency) [NC]
RewriteCond %{REQUEST_URI} ^/$ [OR]
RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R=301]
Adding one more approach of htaccess rules here, written as per shown samples. Using Apache's THE_REQUEST variable here.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(?:index\.php)?/?(?:\?(?:main_page|currency))?\s [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R=301]
OR as an alternative use following code(using QSD flag here to remove query string):
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(?:index\.php)?/?(?:\?(?:main_page|currency))?\s [NC]
RewriteRule ^ %{REQUEST_URI} [L,R=301,QSD]
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
We have a RewriteMap that contains the following examples:
/home /
/index?q=video /videos
/index?q=media /media
/epic.html /epic
How can we RewriteMap to match urls that may or may not have a QUERYSTRING attached to it?
We are currently using:
RewriteMap mappings txt:/data/redirect_mappings
RewriteCond ${mappings:$1|Unknown} !Unknown
RewriteRule ^(.*)$ ${mappings:$1?%{QUERY_STRING}|$1?%{QUERY_STRING}} [L,R=301,QSD]
However, it does not seem to be taking any effect. What am I doing wrong?
Give this a shot
RewriteMap mappings txt:/data/redirect_mappings
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} (.*)
RewriteCond ${mappings:%1|NOTFOUND} !NOTFOUND
RewriteRule ^(.*)$ ${mappings:%1}? [L,R=301]
RewriteCond ${mappings:$1|NOTFOUND} !NOTFOUND
RewriteRule ^(.*)$ ${mappings:$1} [L,R=301]
I must be an idiot because I just can't work this bit out.
I've got a URL:
www.site.com.au/products/product-name.html
I need to redirect these to:
www.site.com.au/product-name.html
All the links are dynamic, the folder doesn't exist. What ReWrite rule do I use to accomplish this?
This is what I've got so far:
RewriteCond %{HTTP_HOST} ^(www|test)\.site\.com\.au
RewriteCond %{REQUEST_URI} ^(/products/)
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^.+\.html$ ${lc:%{REQUEST_URI}} [NC,R=301,L]
Just need to add the bit to remote /products
Thanks.
RewriteRule ^products(/.*)$ http://www.site.com.au$1 [L, R=301]
This replaces everything you listed, except for the first RewriteCond (to match the domain, though if your VirtualHost only answers on those two domains, you can exclude that RewriteCond to simplify it).
RewriteRules are matched first before Apache looks at the RewriteConds, so if you can do the match in the RewriteRule itself it greatly simplifies things. Just for your future reference, if you did need to match in the RewriteCond, it would look something like this:
RewriteCond %{REQUEST_URI} ^/products(/.*)$
RewriteRule ^.*$ http://www.site.com.au%1 [L, R=301]
Note the %1 for matching what's in the parentheses in the RewriteCond vs. the $1 for matching what's in the RewriteRule.
EDIT: Per your comment, the following modification should force lowercase. I haven't had to do that myself, but per this Apache documentation it's an internal function via RewriteMap. Based on your original code it looks like maybe you already have the RewriteMap definition elsewhere. If not, I've included it here.
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} ^/products(/.*)$ [NC]
RewriteRule ^.*$ http://www.site.com.au${lc:%1} [L, R=301]
OK, I really don't know enough about Apache Rewrite to figure out the exact formatting. But after much ado these are the results that worked:
# Lowercase all /products/
RewriteCond %{HTTP_HOST} ^(www)\.site\.com\.au
RewriteCond %{REQUEST_URI} ^/products/.+\.html
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^ ${lc:%{REQUEST_URI}} [R=301,L]
# Lowercase all /products/ and strip products/ subfolder
RewriteCond %{HTTP_HOST} ^(www)\.site2\.com\.au
RewriteCond %{REQUEST_URI} ^/products/.+\.html
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^products/(.+\.html)$ /${lc:$1} [R=301,L]
Thanks,
Dom
Hi guys trying to get a 301 redirect working and having trouble. I need to redirect sub.domain1.com to www.domain2.com and make sure that any file names or parameters get sent over with it.
This is what I was trying:
RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
I also tried this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^sub\.domain1\.com$ /www.domain2.com? [R=301,NE,NC,L]
Where am I messing up?
You missed the subdomain part and proper escaping.
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
Further explain can be found in this question.
Rule of thumb for rewriterules: from the most complex to the less complex.
And don't forget the QSA directive (QSA = Query String Append = "make sure that any file names or parameters get sent over with it")
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [QSA,R=301,L]
Tell me if it works.