So many questions come close to it, but not quite there, and I'm afraid regex makes my head spin!
After a site recode, which slightly amended some urls - which we don't want to go 404 in Google, I am looking for a 301 redirect to remap:
http://www.oursite.com/listing-product-name-1234.html
to
http://www.oursite.com/product-name-1234.html
...you can see we just want to drop "listing-". The "1234" is a unique ID per product, and the product name can be multiple words (hyphen separated) by the way.
Thanks very much for the help regex heads!
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^listing-(.+?\.html)$ /$1 [L,R=301,NC]
Try this:
RewriteCond %{REQUEST_URI} ^/listing-(\w+-)+\d+\.html
RewriteRule ^listing-(.*) $1
Related
I'm trying to write a mod_rewrite rule to do the following:
Request URL:
https://dev.website.com/user/name
to work as
https://dev.website.com/foo/bar/user/name
This is my current .htaccess mod_rewrite, but it's not working.
Regex tools are matching the string, but apache isn't.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^.*\/user\/(.+)*$ ./foo/bar/user/$1
revo's answer in this comment worked.
RewriteRule ^.*/(user/[^/]*)/?$ /foo/bar/$1 [L]
Trying to do a simple URL rewrite with .htaccess but it does not seem to be working.
Want to access http://www.example.com/shop/index.php?route=information/information&information_id=11
using the URL http://www.example.com/MyGreatClub2013
This is what I have in my .htaccess which is stored at the www.example.com root level
RewriteEngine On
RewriteRule ^/MyGreatClub2013$ /shop/index.php?route=information/information&information_id=11 [NC,L]
Am I doing something stupidly wrong?
Remove leading slash:
RewriteEngine On
RewriteRule ^MyGreatClub2013/?$ /shop/index.php?route=information/information&information_id=11 [NC,L,QSA]
mod_rewrite rules when used in .htaccess don't match leading forward slash as .htaccess is per directory.
Here's a decent guide: http://www.javascriptkit.com/howto/htaccess.shtml
Hope it helps.
It's my first request here and hopefully I won't upset anyone.
Here's my story:
I have looked all over this place for a solution and wasn't able to find one. Here's hoping that someone can give some input.
I've basically managed to use Apache's mod_rewrite to create SEO-Friendly urls for my website.
E.g. Old path www.hostname/index1.php?session=user is now rewritten as www.hostname/user, which results into a SEO Friendly URL address.
However, the old path is still valid. I need to somehow redirect all the incoming old index1.php requests to the newer URLs, the SEO-Friendly ones, for the search engines to transfer the link popularities to the new ones. I believe I may have an infinite-loop redirect and that's why it's not working.
My code so far:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index1.php?session=user [QSA,L]
The above rules never get hit when the htaccess file is parsed.
It hit me that I might be doing some sort of redirect loop here so I thought about renaming the index1.php file to index2.php and create something like:
## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index2.php?session=user [QSA,L]
However, that failed too.
What would be the best approach to this? What am I doing wrong here?
Thank you!
Update your .htaccess rules to
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Redirect still valid old links to SEO friendly ones
RewriteCond %{QUERY_STRING} !no-redir [NC]
RewriteCond %{QUERY_STRING} session=user [NC]
RewriteRule ^/?index1\.php$ /user? [R=301,NC,L]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index1.php?session=user&no-redir [QSA,NC,L]
You can't match against the query string (everything after the ?) in a rewrite rule, so you can't match against the session= part. You also can't simply match against the %{QUERY_STRING} var because that gets populated by you other rule when it rewrites the SEO friendly URL to the one with the query string. So you need to match against the actual request:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index1\.php\?session=([^&]+)&?([^\ ]*)
RewriteRule ^ /%2?%3 [L,R=301]
I have the following Rules setup in my .htaccess file
RewriteRule ^detail.php / [R=301,NC,L]
RewriteRule ^tempate.php / [R=301,NC,L]
What these do is perform a 301 redirect when detail.php and template.php is called, although this works this is not working when I enter mydomain.co.za/detail.php?product_id=2432&category=700 then it redirects the website to mydomain.co.za/?product_id=2432&category=700
I need any url where the filename is detail.php or template.php with any amount of parameters in the query string to redirect to the home page
I tried RewriteRule ^detail.php?(.*) / [R=301,NC,L] and this also not working. Any help or guide will be appreciated.
Replace your 2 RewriteRule lines with this line:
RewriteRule ^(?:detail|template)\.php$ /? [R=301,NC,L]
Note that question mark after /, that is a special mod_rewrite syntax to strip out any existing query string from original URI.
Also remember that RewriteRule only matches URI without query string therefore your attempt of ^detail.php?(.*) / [R=301,NC,L] won't work as you expected.
I'm searching for a 301 redirect rules in .htaccess for the last one week. I have visited many similar pages here too. But all are not working for me. So I'm asking help from masters like you.
I'm using wordpress blogging platform. Before I was using Joomla and that time so many 404 errors was there on my website. There are more than 10,000s of 404 errors. Mainly two category of 404 errors. Examples are example.com//component/option,com_xmap/Itemid,44/component/xxx/yyy/menu1/menu2/something/ and example.com/index.php/component/option,com_xmap/Itemid,44/component/xxx/yyy/directory/menu1/menu2/something.html. Like these too many urls. I think this happened due to some cache problem in Joomla and these urls were created one by one on the homepage of a module. After I disabled cache for that module the problem was solved, but google indexed all these bad urls already.
I think it's better to do a 301 redirect for those two categories of 404 error urls to the homepage or to www.freezonal.com/sitemap.xml since these urls are only indexed by google and are not seen in search results.
Currently I'm using this on my .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{QUERY_STRING} .*
RewriteRule ^index.php/(.*)$ /? [R=301,L]
Is there any 301 redirection code for redirecting those two categories of 404 urls to the main website and is there any other suggestion for better SEO. I'll be very thankful to you, if you solve this.
First, WordPress' rewrite rules must be on the bottom, because they slurp up any 404s. Anything after them isn't going to work correctly. So add new rules above WordPress' rules.
RewriteEngine On
RewriteBase /
RewriteRule (index.php/|/)?component/option http://example.com/ [L,R=301]
Something like that should work. I don't know if you want to keep any of your /component/ URLs working, so you may need to remove the question mark from the RewriteRule.
In order for the rewrite to work, make sure that you wrap your rewrite conditions in the mod_rewrite.c tags. Then turn turn on RewriteEngine and set your RewriteBase. I don't think the redirects work outside of the mod_rewrite tags.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} .*
RewriteRule ^index.php/(.*)$ /? [R=301,L]
</IfModule>
If you only want to only redirect these two paths to the homepage, try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^component* / [R=301,L]
RewriteRule ^index.php/component* / [R=301,L]
</IfModule>
Try that out and see if it works.
Also, be sure to check out the documentation on apache.org here http://httpd.apache.org/docs/current/mod/mod_rewrite.html