Htaccess rewrites
-
We’re using wordpress, and we have the following in the .htaccess:
BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
END WordPress
We have a URL like http://subdomain.example.com/select-a-card/?q=fuel, and I want to rewrite it so that it becomes http://subdomain.example.com/fuel-card/
What do I need to add to the htaccess to do this??
Thanks,
-
I managed to strip out the '?q=' query with the htaccess below, but unfortunately this breaks the query. Any way around this?? (http://subdomain.example.com/select-a-card/?q=fuel)
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^q=fuel$
RewriteRule ^(.*)$ /fuel/? [R=301, L]BEGIN WordPress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]END WordPress
-
This should work:
RewriteRule ^([^/]*)/$ /select-a-card/?q=$1 [L]
This will redirect http://subdomain.example.com/select-a-card/?q=__anyname to http://subdomain.example.com/anyname__/. If you just want to 301 the one page, try this:
Redirect 301 /select-a-card/?q=fuel http://subdomain.example.com/fuel-card/
-
Ok, so how about if I make the ask a bit easier...
Whilst not an ideal solution, it would be good if I could just rewrite
http://subdomain.example.com/select-a-card/?q=fuel
to
http://subdomain.example.com/select-a-card/fuel/
...or ideally with a trailing slash too: http://subdomain.example.com/select-a-card/fuel/
-
No it wont be conflicting with WordPress, just a matter of getting the syntax correct. I'll try and amend my initial solution later this evening and report back, thanks in advance for your patience.
-
Hi Ben,
Thanks for replying. Unfortunately I've tried the above and it's not working.
I've tried escaping everything, but still no joy. Could this be clashing with wordpress permalink redirects perhaps?
-
Try this:
BEGIN WordPress
<ifmodule mod_rewrite.c="">RewriteEngine On
RewriteBase /
# Custom Rewrite Rules
RewriteCond %{REQUEST_URI} ^/select-a-card/$ [NC] RewriteCond %{QUERY_STRING} ^q=fuel$ RewriteRule ^index.php$ /fuel-card/? [R=301,NC,L]
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]</ifmodule>END WordPress
Got a burning SEO question?
Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.
Browse Questions
Explore more categories
-
Moz Tools
Chat with the community about the Moz tools.
-
SEO Tactics
Discuss the SEO process with fellow marketers
-
Community
Discuss industry events, jobs, and news!
-
Digital Marketing
Chat about tactics outside of SEO
-
Research & Trends
Dive into research and trends in the search industry.
-
Support
Connect on product support and feature requests.
Related Questions
-
Htaccess noob
Hi all, I discovered that a site I'm working on has some pages from their old site that are stil live and getting traffic. I would like to add redirects for them to the .htaccess file. Unfortunately, I have zero experience writing or editing .htaccess files. Here is the existing .htaccess file: BEGIN WPSuperCache END WPSuperCache BEGIN WordPress <ifmodule mod_rewrite.c="">RewriteEngine On
Technical SEO | | rich.owings
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]</ifmodule> END WordPress And here are a few sample lines that I need to add: Redirect 301 /files/John-Doe.html /staff/
Redirect 301 /files/Jane-Smith.html /staff/
Redirect 301 /files/contact.html /contact/ So my question is, should I place those lines immediately after Rewrite engine on, or just before the closing IfModule tag or...? There is no consistent pattern that would allow for a single rule or two. Oh, and I will backup the current .htaccess file first! Many thanks for any help anyone can offer.0 -
.htaccess Question
Hi,I have a website www.contractor-accounts.co.uk that has an .htaccess file that strips .php and forces a closing brace /. The site is now over 6 months old and still has a very low ranking with MOZ also rating the site as DA/PA = 1 which seems to indicate some sort of issue with the website. Can anyone offer any suggestions as to why this site is ranking poorly as much of the onpage SEO has been completed to a level of 90%+ for specific keyterms so I'm probably either looking at routing of the framework of so other technical SEO issues possibly? Any help much apreciated... <ifmodule mod_rewrite.c=""><ifmodule mod_negotiation.c="">Options -MultiViews</ifmodule> RewriteEngine On # Redirect Trailing Slashes...
Technical SEO | | ecrmeuro
# RewriteRule ^(.)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} /+[^.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
# Redirect non-WWW to WWW...
RewriteCond %{HTTP_HOST} ^contractor-accounts.co.uk [NC]
RewriteRule ^(.)$ http://www.contractor-accounts.co.uk/$1 [L,R=301] # Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]</ifmodule>0 -
.htaccess yikes!
Hi guys, Having a bit of trouble getting a htaccess redirect working. It's very easy but I am making a small typo somewhere! I want to redirect about 400 product pages located at; http://domain.com/product/product-name to http://domain.com/product-name ..basically just dropping /product/ Any help would be appreciated as my errors in Moz are all over the place!
Technical SEO | | MrPenguin0 -
Looking for a tool that will display the contents of the htaccess file
I am looking for an online tool that will display the contents of the htaccess file. i came across a tool a month ago, but I can not recall the name of the tool. Thanks
Technical SEO | | irvingw0 -
Duplicate Page Titles Warnings, htaccess Rewrite & Canonical Links.
Hey guys, Just signed up for a pro account and I am getting Duplicate Page Title warnings on links that are duplicate, rewritten for SEO, but have a canonical href tag. I have two sets of links in my store: SEO friendly: http://www.mysite.com/item/iphone-case Operational link: http://www.mysite.com/shop/product.php?pid=11 This operational link however has a href canonical tag pointing to the SEO friendly link as being the primary link. My question is; Do I need to worry about this Duplicate Page Title Warning if I am using a canonical tag on the Operational link pointing to the SEO friendly link? Thanks!
Technical SEO | | jason3600 -
HTACCESS redirect vs. forwarding
I'm having trouble using htaccess redirect to redirect a subdomain to a new domain on a different server. Tech support at godaddy suggested I forward the subdomain. The subdomain has already been cached by google. Will forwarding in this way have the same affect (SEO wise) as an htaccess redirect??
Technical SEO | | triple90 -
Htaccess redirects how many can i have and does it slow site down
I have had to redesign my site this year www.in2town.co.uk because my hosting company made a huge mistake while trying to update the joomla on my site which resulted in me losing thousands of pages and links. What i would like to know is, i have put some of the old urls in my htaccess file but i would like to know how many old urls i can have in my htaccess file as i am unsure how to use it properly. my idea was, to have some of the lost url links to my site and put them in my htaccess file and have them pointing to similar pages. not sure if this is a good idea or not. i think i have lost a few hundred good links but i would like to know if these urls in the htaccess file would slow down my joomla site any advice would be great.
Technical SEO | | ClaireH-1848860 -
Htaccess issue
I have some urls in my site due to a rating counter. These are like: domain.com/?score=4&rew=25
Technical SEO | | sesertin
domain.com/?score=1&rew=28
domain.com/?score=5&rew=95 These are all duplicate content to my homepage and I want to 301 redirect them there. I tried so far: RedirectMatch 301 /[a-z]score[a-z] http://domain.com
RedirectMatch 301 /.score. http://domain.com
RedirectMatch 301 /^score$.* http://domain.com
RedirectMatch 301 /.^score$.* http://domain.com
RedirectMatch 301 /[a-z]score[a-z] http://domain.com
RedirectMatch 301 score http://domain.com
RedirectMatch 301 /[.]score[.] http://domain.com
RedirectMatch 301 /[.]score[.] http://domain.com
RedirectMatch 301 /[a-z,0-9]score[a-z,0-9] http://domain.com
RedirectMatch 301 /[a-z,0-9,=,&]score[a-z,0-9,=,&] http://domain.com
RedirectMatch 301 /[a-z,0-9,=&?/.]score[a-z,0-9,=&] http://domain.com None of them works. Anybody? Solution? Would be very much appriciated0