Using mod_rewrite / RewriteRule to create SEO friendly URLs - .htaccess

I'm trying to get this unfriendly link:
http://test.mysite.com/featured1new.php?homedetails=12-Magnolia-Court-Branson-MO-65616&ID=11315943&PHOTOID=20140401022411163178000000
To display as this friendly link when using my website:
http://test.mysite.com/homedetails/12-Magnolia-Court-Branson-MO-65616/ID/11315943/PHOTOID/20140401022411163178000000
This is the code in my .htaccess file in the main directory:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule homedetails/(.*)/ID/(.*)/PHOTOID/(.*)/ featured1new.php?homedetails=$1&ID=$2&PHOTOID=$3
RewriteRule homedetails/(.*)/ID/(.*)/PHOTOID/(.*) featured1new.php?homedetails=$1&ID=$2&PHOTOID=$3
The problem is the link still displays the old way in the website, however, if I key the new RewriteRule way directly into the browser, it works fine. So part of this is working the way I need it too.
Am I supposed to change my html code in the website to match/use the RewriteRule? (After reading on this site for quite some time, I didn't think I needed to do that) Thanks for any help

The problem is the link still displays the old way in the website
Your rule simply allows new url format and internally rewrites it to its old format equivalent.
If you want old format to redirect to new format (without an infinite loop), you have to add some code.
You can replace your current code by this one
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/featured1new\.php\?homedetails=([^&\s]+)&ID=([^&\s]+)&PHOTOID=([^&\s]+)\s [NC]
RewriteRule ^ /homedetails/%1/ID/%2/PHOTOID/%3? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^homedetails/([^/]+)/ID/([^/]+)/PHOTOID/([^/]+)/?$ /featured1new.php?homedetails=$1&ID=$2&PHOTOID=$3 [L]
NB: even if this code redirects old format, it's better to replace your links with new format

Related

redirecting with htaccess causes too many redirects

The website I'm working on is using some cms. I need to add a static website to this. When I put mypage.html in the main directory and go to www.website.com/mypage.html it works. I would like the page to be accessible without '.html' ending. I experimented with editing htaccess files but always end up with error of too many redirections.
What I entered were various combinations, for example
Redirect 301 http://website.com/mypage http://website.com/mypage.html
The htaccess file I'm using looks like this:
:Location /*.php
Use php54
:Location
RewriteEngine On
DirectoryIndex index_prod.php
Options -Indexes
RewriteRule ^.*\.(css|png|swf|js|gif|jpeg|jpg|flv|pdf|doc)$ - [L]
RewriteRule ^net2ftp - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^/?$ plug.html [L]
#RewriteCond %{REQUEST_URI} !=/
RewriteRule ^/?.* index_prod.php
I'm looking for tips or to be explicitly told what and where to put in htaccess file to make it work (if it's possible)
Could you please try following, considering that you want without extension file URLs to be served by html extension files. Also since you didn't mention any specific condition before RewriteRule hence that redirection errors are coming to it, because its keep on redirecting in lack of any condition/check's presence(till its maximum redirection limit is crossed).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [NC,L]

Url rewriting in folder project

I use subdomains a lot for webs and everytime i use url rewriting like this one:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?key=$1
so if i use sudomain.domain.com/something its ok and it shows me index.
But now i try to use it on website which i have only in folder so adress is like.
domain.com/newwebsite/
i need same system here, so if i put domain.com/newwebsite/somepage it will be ok and it shows me index page like example before.
Try this rule in /newwebsite/.htaccess:
RewriteEngine On
RewriteBase /newwebsite/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?key=$1 [L,QSA]

URL rewritten but indexed as Query String

I tried googling this but all it gave me was tutorials on how to rewrite so I thought I'd give it a shot asking it here.
The problem is that we have a htaccess file that rewrites the urls to a query string:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\/]+\/*[^\/]*\/*.*)$ index.php?app_route=$1 [QSA,L]
This works and it seems google is picking it up, however when you click on the link in google it shows the query string that it's rewritten to. We only link to the 'nice' urls so we don't know where google picked this up. Google for site:sorellehaarmode.nl to see for yourself.
Does anyone have experience with, or a solution to this problem?
Try adding these rules (anywhere to your current htaccess file, below the rules you already have is fine):
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?app_route=([^&\ ]+)&?([^\ ]*)
RewriteRule ^/?index\.php$ /%1?%2 [L,R=301]
This should permanently point the URLs with query strings to the ones without.

using - instead of _ (underscore) with .htaccess

I'm using with good results the following code to access alla of my php files into the /it directory without specifying the extension. In other words I can access to "http://www.mydomain.com/it/about.php" just writing "http://www.mydomain.com/it/about".
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.mydomain.com/it/$1.php [L]
the same happen when i try to access to http://www.mydomain.com/it/question_answers.php.
How can I access directly to *"http://www.mydomain.com/it/question_answers.php"* also writing "http://www.mydomain.com/it/question-answers"?
I wrote the floowing code below the previous but it seems not to work.
Redirect 301 /question-answer http://www.mydomain.com/it/question_answer.php
because if i write "http://www.mydomain.com/it/question-answer" the browser try to open the page:
"http://www.mydomain.com/it/question-answer.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php"
A small abstract of the post:
I have the page *"http://www.mydomain.com/it/question_answers.php"*
with the first part of code I can get it using the link *"http://www.mydomain.com/it/question_answers"*
I'd like to access the same page also with the following "http://www.mydomain.com/it/question-answers"
Thanks!
This should work for you:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.mydomain.com/$1.php
RewriteRule it/question-answer\.php http://www.mydomain.com/it/question_answer.php [R=301,L]
I have changed two things: I deleted it/ in the new URL of the first RewriteRule. Otherwise you would be redirected to it/it/
I also added \.php to the second RewriteRule. I don't really know why, but the RewriteRule seems to replace the pattern instead of redirecting. And if your pattern is it/question-answer and the real url is it/questions-answer.php the .php will not be replaced.

htaccess and URL rewriting

I have a list of URLs such as,
http://www.mywebsite.com/page.php?genus=A_GENUS&species=A_SPECIES&id=12345.
I would like to write a .htaccess which permanently redirects visits to this form of URL to the following URL,
http://www.mywebsite.com/species/A_GENUS/A_SPECIES.
Is it possible to do this without having to manually list each species in the database?
I've tried to look it up but my head is in WordPress-Custom-Post-Type land and as such my brain isn't functioning properly. Any help would be greatly appreciated.
EDIT: Clarification
Currently my .htaccess is completely empty. I am re-writing my website to use an entirely new CMS and this new URL format. The old URL format will cease to exist but all of the information will still be used.
We are quite highly ranked for a lot of species on Google and I would like visitors from there to be able to view the information they require despite the URL format changing.
These changes haven't occurred yet (still using a Sandbox environment for the new version of the site) and I'd like to make the URL changes just before I "go live" with the new version.
EDIT 2: New site .htaccess
The contents of the new site's .htaccess looks like this in its entirety:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sandboxfolder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /sandboxfolder/index.php [L]
</IfModule>
# END WordPress
EDIT for Garmen's answer
RewriteEngine On
RewriteBase /sandboxfolder/
RewriteCond %{QUERY_STRING} genus=([a-zA-Z0-9-]+)&species=([a-zA-Z0-9-]+)
RewriteRule ^profile.php$ /species/%1/%2 [R=302]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sandboxfolder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /sandboxfolder/index.php [L]
</IfModule>
# END WordPress
Not sure if I've used RewriteBase correctly there, but it doesn't appear to function with or without it.
Regards,
RewriteEngine On
RewriteCond %{QUERY_STRING} genus=([a-zA-Z0-9-]+)&species=([a-zA-Z0-9-]+)
RewriteRule ^page.php$ /species/%1/%2? [L,R=302]
This one assumes a very specific order of query string parameters. So be warned. Also it assumes the names only contains plain letters, numbers, or dashes.
Change 302 to 301 when you are done testing. I used 302 because 301's are aggressively cached by browsers, making debugging very difficult.
EDIT: You should add this above the other rewrite rules you have, or it will not work.
EDIT 2: added a ? at the end to remove the querystring. And L flag to prevent further execution.

Resources