How can I modify the .htaccess file and get SEO friendly URLS instead of a query string. I want to achieve this 3 goals:
localhost/example/products/ instead of
localhost/example/products-list.php
localhost/example/products/38/ instead of
localhost/example/products.php?id=38
localhost/example/products/38/red/ instead of
localhost/example/products.php?id=38&color=red
On another post #anubhava helped me a lot and this is what I have right now for the second point:
RewriteEngine on
RewriteBase /example
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /products(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ products/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([^/]+)/?$ products.php?id=$1 [L,QSA]
The second point works properly but I want to know where do I have to put the other rules, before or after. I put this right after the other rule, but it's not working because it redirects to the previous url localhost/example/products/38/:
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /products(?:\.php)?\?id=([^\s&]+)\?color=([^\s&]+) [NC]
RewriteRule ^ products/%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([^/]+)/([^/]+)/?$ products.php?id=$1&color=$2 [L,QSA]
You need new set of rules for 2 parameters:
RewriteEngine on
RewriteBase /example/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /products(?:\.php)?\?id=([^\s&]+)&color=([^\s&]+) [NC]
RewriteRule ^ products/%1/%2/? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /products(?:\.php)?\?id=([^\s&]+)\s [NC]
RewriteRule ^ products/%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# internal forward from pretty URL to actual one
RewriteRule ^products/([^/]+)/?$ products.php?id=$1 [NC,L,QSA]
RewriteRule ^products/([^/]+)/([^/]+)/?$ products.php?id=$1&color=$2 [NC,L,QSA]
RewriteRule ^products/?$ products-list.php [L,NC]
Related
I've got the following htaccess file:
RewriteEngine On
RewriteBase /
# Redirect to remove .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# Redirect to "page" for dynamic pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ page?url=/$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
This allows my custom CMS to use dynamic URLs (http://example.com/some-page, for example) and redirect it to http://example.com/page?url=some-page so that the CMS can render the content. It all works great - until someone adds a URL like http://example.com/something/else. When I spit out the url parameter with: print $_GET['url']; I get /something/else.php/else.
So it seems like the remove .php directive is getting lost and the second parameter is getting duplicated? Thanks for any help.
Have it this way:
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{ENV:REDIRECT_STATUS} =200
RewriteRule ^ - [L]
# Redirect to remove .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
# Redirect to "page" for dynamic pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule !^page\.php$ page.php?url=%{REQUEST_URI} [L,NC,QSA]
Here are changes:
Keep redirect rule before rewrite rules otherwise when www is removed from a URL then your internal URL will be exposed.
Use page.php in target instead of page to avoid another rewrite rule execution.
Use [L] flag in .php adding rule.
Addition of Options -MultiViews
Hello i want to convert php urls into SEO urls every thing is working fine at my end but the files in the root doesn't work here is the code which i am using in htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ show_staff.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ show_staff.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)$ show_users.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ show_users.php?url=$1
but if i add these lines right after the above lines the files which are in root doenst work without .php extention here is what i want to add here i want to mention without or with using below code the above code works fine for me
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* $0.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
It looks like your stuff is all in the wrong order. Some things:
If you don't have an L flag for a rewrite, the rules after will continue to process the rewritten request. So you want to use the L flag.
Rewrite conditions only apply to the immediately following rule, so you've got a condition that's just dangling somewhere and not being used.
The show_users.php rules will never work the way you've set things up, the regex pattern is the same, so no matter what, your requests will always go to show_staff.php.
Your rules aren't in the right order, two things need to happen if I'm guessing what you're trying to do. You need to match against a request with a php extension and externally redirect the browser, then you need to internally rewrite that request back to the URI with a php extension.
So something like:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \ /+([^/]+)\.php
RewriteRule ^ /%1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
ReweriteRule ^ %{REQUEST_URI}.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9-/]+)/?$ show_staff.php?url=$1 [L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
## hide .php extension
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9/-]+)$ show_staff.php?url=$1 [L,QSA]
When I use following url rewrite from:
http://www.mapsopensource.com/m.php?url=asia-globe-map
to
http://www.mapsopensource.com/asia-globe-map.html
It rewrites quite well but when I combine this with another rule and try to rewrite the following the result is nothing:
http://www.mapsopensource.com/country.php?countryAlias=usa
to
http://www.mapsopensource.com/usa.html
Please guide me as to how I can combine both url rewrites in one htacess for two or more different dynamic urls.
My current code is:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /m\.php\?url=(.*)\ HTTP
RewriteRule ^ /%2.html? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\.html$ /m.php?url=$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /country\.php\?countryAlias=(.*)\ HTTP
RewriteRule ^ /%2.html? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\.html$ /country.php?countryAlias=$1 [L,QSA]
Google is indexing multiple links instead of indexing only one which is a 301 redirect from adding multiple hyphens.
My website url is designed the following way: url.com/id/title-goes-here/ ; If google were to index the following it would be this way..
url.com/id/title-goes-here/
url.com/id/title%20-goes-here/
url.com/id/title%20goes%20here/
so how many hyphens there are it indexes that amount of urls when it should only index 1 which is the hyphens. I have included a 301 redirect but it is still not working..
Here is my .htaccess code..
RewriteEngine on
# add www before hostname
RewriteCond %{HTTP_HOST} ^site\.co$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
#if on article page, get slugs and make into friendly url
RewriteCond %{THE_REQUEST} \s/article\.php\?article_uid=([^&]+)&article_title=([^&\ ]+)
RewriteRule ^ /article/%1/%2/? [L,R=302,NE]
#if page with .php is requested then remove the extension
RewriteCond %{REQUEST_URI} !/images/image_resizer.php
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]
#forces article title to redirect only once with hyphens
RewriteRule "^(article)/([^ ]*) +(.*)$" /$1/$2-$3 [L,R=301]
#Force a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+([^.]+?[^/.])[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]
#allow page direction to change the slugs into friendly seo URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?:^|/)article/([^/]+)/([^/]+)/?$ /webroot/article.php?article_uid=$1&article_title=$2 [L,QSA,NC]
#silently rewrite to webroot
RewriteCond %{REQUEST_URI} !/webroot/ [NC]
RewriteRule ^ /webroot%{REQUEST_URI} [L]
#.php ext hiding
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Why is google still indexing multiple urls even though the hyphen has a 301 redirect?
I'd search first where did Google found this strange URLs. I'm pretty sure there is no need to do this redirects.
Also consider canonical meta header on every page to force Google to use only one way of URL representation selected by you.
For example:
<link rel="canonical" href="http://url.com/id/title-goes-here/"/>
Regardless of fetched page, be it url.com/id/title-goes-here/ or url.com/id/%20title-goes-here/ search result will be url.com/id/title-goes-here/
I have a URL from my website like this -
http://www.mydomain.com/profiles/centers/index.php?code=2507&name=Tharanga+Higher+Educational+Institute#page=art-section
I need to display above url as friendly url on my browser's address bar when it going to above page.
My expecting url something like this -
http://mydomain.com/centers/Tharanga-Higher-Educational-Institute#page=art-section
This code so far in my .htaccess file
# Enable Rewriting
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /profiles/centers/index.php\?code=([0-9]+)&name=([^&]+)&?([^\ ]+)
RewriteRule ^profiles/centers/index\.php /%1/?%2 [R=301,L,NE]
But this code is not working for me. hope someone will help me out.
Thank you.
Replace your code with this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+profiles/centers/index\.php\?code=([^&]+)&name=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/([^/]+)/?$ /profiles/centers/index.php?code=$1&name=$2 [L,NC,QSA,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(js|css|jpe?g|gif|png)$ /profiles/centers/%{REQUEST_URI} [NC,L,R=302]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.