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
-
Redirect chain error free htaccess code for website
i want to redirect domain, example.com to https://www.example.com, is anyone can help me to provide redirect chain error free ht-access code. I implemented this htaccess code on the website and mhy site show on the moz redirect chain error RewriteCond %{HTTP_HOST} !=""
Technical SEO | | truehab
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.)\sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s(./)/+\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L]0 -
What's Worse - 404 errors or a huge .htaccess file
We have changed our site architecture pretty significantly and now have many fewer pages (albeit with more robust content and focused linking). My question is, what should I do about all the 404 errors (keep in mind, I am only finding these in Bing Webmaster tools, not Moz or GWT)? Is it worse to have all those 404 errors (hundreds), or to have a massive htaccess file for pages that are only getting hits by the Bing crawlbot. Any insight would be great. Thanks
Technical SEO | | CleanEdisonInc0 -
Is it necessary to 301 rewrite /index.php to /?
Hi, We have build a lot of external link to http://www.oursite.com/ Do I have to do a 301 redirect from http://www.oursite.com/index.php to http://www.outsite.com/? Thanks
Technical SEO | | LauraHT0 -
Lowercase Rewrite in web.config. Strange behavior.
So I am trying to implement a lower case rule in my rewrite section and I am using the same snippet below that can be found all over the web. The lower casing of my url works fine but it adds my domain name into the URL for reasons I can't explain. so this www.SomeSite.com/Upper.aspx become www.somesite.com/somesite/upper.aspx I removed all other rewrite rules but this one and still it happens. Has anyone seen this? My server is IIS 7 <match url="[A-Z]" ignorecase="false"></match> <action type="Redirect" url="{ToLower:{URL}}"></action>
Technical SEO | | Banknotes0 -
Removing URL Parentheses in HTACCESS
Im reworking a website for a client, and their current URLs have parentheses. I'd like to get rid of these, but individual 301 redirects in htaccess is not practical, since the parentheses are located in many URLs. Does anyone know an HTACCESS rule that will simply remove URL parantheses as a 301 redirect?
Technical SEO | | JaredMumford0 -
Mod Rewrite / .htaccess avoid duplicate content
I have been searching and testing for hours but cannot find a solution. I am able to get a URL to display with out the file exntension. i.e domain.com/file instead of domain.com/file.php The problem is both versions of the URL above work, therefore a duplicate content issue. How can I force the URL with the file extension not to resolve and give a 404 error? Or just redirect to the non extension URL? IF it helps here is my code. Options +FollowSymLinks
Technical SEO | | MiamiWebCompany
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]0 -
.htaccess help please
Hi there, Can anyone let me know what I need to add to my .htaccess file to make sure that the trailing slash on my domain gets redirected to my main domain name. For example, at the moment Google is seeing my primary domain as follows: http://www.mydomain.com http://www.mydomain.com/ I thought I'd covered off all possible duplication penalities but it look as though I've still got one left! Thanks very much for your help, Simon
Technical SEO | | theshortstack0 -
.htaccess problem using POST method
Hi guys I'm after some help with trying to achieve the following: 1. Canonicalise to http://www. 2. Remove the index.php from root and subfolders. I have the .htaccess code below, which seemed to work fine, but the urls use the POST method and this isn't working with the rewrites. Can anyone please advise as to what I am doing wrong? As you can probably guess .htaccess isn't my strongest SEO discipline! The code I have is: http:// to http://www. RewriteEngine on
Technical SEO | | TrevorJones
RewriteCond %{HTTP_HOST} ^mydomainexample.com
RewriteRule (.*) http://www.mydomainexample.com/$1 [R=301,L] /index.php to / Options +FollowSymLinks
DirectoryIndex index.php RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.php\ HTTP/
RewriteRule ^index.php$ http://www.mydomainexample.com/ [R=301,L] Subdirectory /index.php to / RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)index.(php|html|htm?)[#?]?
RewriteRule ^(([^/]+/))index.(php|html|htm?)$ http://www.mydomainexample.com/$1 [R=301,L] Just to add to this I have found this which I think is what I need to restrict it to GET: RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L] Thank you in advance for any suggestions as to how I may put this code together.. Trevor0