Hello everyone i am new in .htaccess URL rewriting, i have URL as like
http://localhost/pune99/Movie/xyz+pvt+ltd
but i want it as like this
http://localhost/pune99/xyz-pvt-ltd
thanks in advance...
my .htaccess code
#Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
# executes repeatedly as long as there are more than 1 spaces in URI
RewriteRule "^(\S*)\s+(\S*\s.*)$" /$1-$2 [L,NE]
# executes when there is exactly 1 space in URI
RewriteRule "^(\S*)\s(\S*)$" /$1-$2 [L,R=302,NE]
#RewriteBase /
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
# To externally redirect /dir/foo.php?name=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?name=([^&\s]+) [NC]
RewriteRule ^ %1/%2? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?name=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)?$ $1.php?name=$2 [L,QSA
Change your first 2 rules to this so that you can convert both + and space to hyphens:
# executes repeatedly as long as there are more than 1 spaces in URI
RewriteRule "^([^\s+]*)[\s+]+([^\s+]*[\s+].*)$" /$1-$2 [L,NE]
# executes when there is exactly 1 space in URI
RewriteRule "^([^\s+]*)[\s+]+([^\s+]*)$" /$1-$2 [L,R=302,NE]
Related
I have my htaccess file setup, so that the pages remove extensions. Now, I am trying to make the pages that transfer variables, into SEO friendly urls ... so, for example...
http://www.example.com/art-gallery?page=2 ... which is actually "art-gallery.php?page=2", would turn into... http://www.example.com/art-gallery/page/2
Or... http://www.example.com/art-piece?id=3 ...would go to... http://www.example.com/art-piece/id/3
... and so on ...
I have alot in my htaccess file, and am not sure how to do the above (there are plenty of tutorials on going from www.example.com/index.php?page=2 to www.example.com/page/2/ but none that do exactly what I need). Ideally, I'd like to be able to do this for all similar pages...
# enable the rewrite engine
RewriteEngine On
# Set your root directory
RewriteBase /
# Force www:
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
# Remove the .php extension
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# Remove index and reference the directory
RewriteRule (.*)/index$ $1/ [R=301]
# Remove trailing slash if not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# Forward request to html file, **but don't redirect (bot friendly)**
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
# Disable Directory Browsing
Options -Indexes
# Disable Hotlinking of Images
# with forbidden or custom image option
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]
# Protect htaccess File
<files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</files>
You can transfer parameters with the variable QUERY_STRING.
Consider the following rule:
RewriteRule ^index.html index.php?%{QUERY_STRING}&m=main&a=index
This rule would transform
index.html?something=value
into
index.php?something=value&m=main&a=index
You should use the RewriteEngine.
You could also use a 301 redirect either alone or in conjunction with the RewriteEngine to redirect SEs.
Generally, though redirecting SEs to a different page than what users will see is not a good practice, and may result in your pagerank decreasing. Instead, try migrating all your pages to the second URL format, and consider using 301 redirects to help the transition.
Generally: Use 301 redirects for SE-friendly page changes. See this SO for additional reference.
You can insert this rule just before Forward request to html file rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/])/([^/])/([^/])/?$ $1.php?$2=$3 [L,QSA]
This is quite old but why not do the following:
RewriteEngine On
RewriteRule ^([^?]*) index.php?route=$1 [L,QSA]
Then in your index.php you can handle it like such;
if (isset($_GET['route'])) {
$route = explode('/', $_GET['route']);
if (iconv_strlen((end($parts)), 'UTF-8') == 0) {
array_pop($parts);
}
}
From here your main level would be handled with $route[0], second level $route[1]
For example;
http://example.com/art-gallery/2
$route[0] would equal 'art-gallery'
$route[1] would equal '2'
I have the following .htaccess file:
AddDefaultCharset utf-8
RewriteEngine on
Options +SymLinksIfOwnerMatch
RewriteBase /
# redirect all www-requests to no-www
# -
RewriteCond %{HTTP_HOST} ^www\.site\.com$ [NC]
RewriteRule ^(.*)$ http://site.com/$1 [R=301,L]
# redirect all home pages to / (root)
# -
RewriteCond %{THE_REQUEST} ^.*/index\.(php|html?)
RewriteRule ^(.*)index\.(php|html?)$ /$1 [R=301,L]
# remove trailing slash from dirs
# -
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ /$1 [R=301,L]
# automatically add index.php when needed
# -
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|login\.php|reg\.php|robots\.txt|css/|js/)
RewriteRule ^(.*)$ /index.php [L]
The .htaccess file should do the following (for SEO):
Conversion to no-www (http://www.site.com should become http://site.com)
All URIs with trailing slashes should convert to no-trailing-slash: http://site.com/me/ should be redirected http://site.com/me
All URIs with index.php/index.html should convert to just nothing: http://site.com/admin/index.php or http://site.com/admin/ should be eventually displayed as http://site.com
However the current version of .htaccess results in a cyclic redirection when trying to access (http://site.com/admin). The real document that should be fetched by browser is http://site.com/admin/index.php.
Can anyone please help me with this issue?
There's a module called mod_dir that's automatically loaded and it causes requests for directories that are missing the trailing slash to get redirected with a trailing slash. You can turn this off using the DirectorySlash directive, but note the security warning when you turn it off. There's an information disclosure security issue if you turn it off and default indexes won't get loaded. However, your lats rule (looks like) it does that, though incorrectly.
First, turn off the DirectorySlash
DirectorySlash Off
Then you need to change the last rule to:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^(.*)$ /$1/index.php [L]
I am a newbie here. I am trying to clean up my links. I made an attempt looking at some of the answers here. I am sure I am not even close....!Can you please help me with this?
Currently my link structure is this...
xyz.com/viewgallery.php?cname=Colorado-Fall&pcaption=Touched-By-Light
But I would like the viewer to see this link xyz.com/viewgallery/Colorado-Fall/Touched-By-Light ( This means redirect?)
Then rewrite back to my link. ( ?? This is how I understand it)...
So I wrote...
check if the actual request if for "this1"
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /viewgallery.php\?cname=(.*)&pcaption=(.+)
# redirect to "this2"
RewriteRule ^viewgallery\.php /%1/%2/?%3 [R=301,L]
# now rewrite "this2" back to "this1"
RewriteRule ^(.*?)/(.+)$ /viewgallery.php?cname=$1&pcaption=$2 [L,QSA]
Is this correct? I am sure it looks like gibberish ..sorry about that!...
Thanks for your help..
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 /
# externally redirect /viewgallery.php?cname=Colorado-Fall&pcaption=Touched-By-Light to
# /viewgallery/Colorado-Fall/Touched-By-Light to
# RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(viewgallery)\.php\?cname=([^&]+)&pcaption=([^&\s]+) [NC]
# RewriteRule ^ /%1/%2/%3? [L,R=301,NE]
# internally forward /viewgallery/Colorado-Fall/Touched-By-Light to
# /viewgallery.php?cname=Colorado-Fall&pcaption=Touched-By-Light
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?cname=$2&pcaption=$3 [L,QSA]
Explanation:
Rule 1 is for external redirect to make your URL pretty
It captures "viewgallery" and parameter cname and pcaption's values in %1, %2, %3
Thereafter these captured variables are used to create pretty URL /viewgallery/%2/%3
Rule 2 is for internal forward
From URL: /viewgallery/Colorado-Fall/Touched-By-Light it captures 3 variables: "viewgallery", "Colorado-Fall" and "Touched-By-Light" into $1, $2, $3
Captured variables are used to construct real URL: /viewgallery.php?cname=$2&pcaption=$3
UPDATE:
# /viewgallery.php?cname=Colorado-Fall&pcaption=Poked to /photos/Colorado-Fall/Poked.jpg
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+viewgallery\.php\?cname=([^&])+&pcaption=([^&\s]+) [NC]
RewriteRule ^ /photos/%1/%2.jpg? [R=301,L]
RewriteRule ^photos/([^/]+)/([^.]+)\.jpg$ /viewgallery.php?cname=$1&pcaption=$2 [QSA,L,NC]
For my site I have a directory called /test/. I want to rewrite www.example.com/nl/test and www.example.com/nl/test/ to a certain page (test.php).
Some global conditions (for all the rules)
RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R]
RewriteBase /
RewriteRule ^(nl|en)$ $1/ [NC,R]
RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ en/$2 [L,R=302]
RewriteRule ^(nl|en)/(.*)$ $2?language=$1&%{QUERY_STRING} [L]
RewriteRule ^sale$ sale.php
RewriteRule ^valentine$ valentine.php
Some conditions for the rewrite + folder
RewriteRule ^test/$ test.php
The redirect of www.example.com/nl/test/ is correct. The language parameter is also correctly rewritten.
For the second redirect (the version without the trailing slash) I can't get this working.
RewriteRule ^test$ test.php
Now my URL is rewritten as www.example.com/test/?language=nl
Can someone give me a tip or hint to fix this? I can't change the name of the directory since there are several external URLs linking to this directory.
This rule will do the whole job (instead of 4 lines you have there): it will rewrite both /nl/test and /nl/test/ to /test.php?language=nl.
RewriteRule ^(en|nl)/test/?$ /test.php?language=$1 [NC,QSA,L]
NOTES:
The [QSA] flag will preserve any existing query string (therefore, there is no need for &%{QUERY_STRING}).
Full .htaccess:
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteBase /
RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R=301,L]
RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ /en/$2 [R=302,L]
RewriteRule ^(nl|en)/(.*)$ /$2?language=$1 [NC,QSA,L]
RewriteRule ^sale/?$ sale.php [QSA,L]
RewriteRule ^valentine/?$ valentine.php [QSA,L]
RewriteRule ^test/?$ test.php [QSA,L]
NOTES:
There is no need for RewriteRule ^(nl|en)$ $1/ [NC,R] as you already have RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R=301,L]. It does the same job.
I want to redirect users to my home page if they enter any URL not starting with 'en' or 'fr'. I've spent hours trying to make this work but this proves too tough for my fragile newbie brain. Currently, if I try to go to
http://mysite.com/fr/produits/
Firefox tells me that the redirect will never complete.
Here's what I want:
http://mysite.com/en/whatever/ ==> rewrite as /whatever/index.php?lang=en
http://mysite.com/fr/whatever/ ==> rewrite as /whatever/index.php?lang=fr
http://mysite.com/jp/whatever/ ==> 301 redirect to /fr/accueil/
http://mysite.com/whatever/ ==> 301 redirect to /fr/accueil/
Here's my current htaccess. See inline comments for details.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Require no www
RewriteCond %{HTTP_HOST} !^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301]
# Redirect from root to /fr/accueil/, the URL for the default home page
RewriteRule ^/$ http://mysite.com/fr/accueil/ [NC,R=302]
# Rewrite language path fragment (/fr or /en) as a parameter
RewriteRule ^fr/accueil/$ /accueil/index.php?lang=fr [QSA,NC]
RewriteRule ^en/home/$ /accueil/index.php?lang=en [QSA,NC]
RewriteRule ^fr/produits/$ /produits/index.php?lang=fr [QSA,NC]
RewriteRule ^en/products/$ /produits/index.php?lang=en [QSA,NC]
# I'm aware that the rules in this htaccess are re-examined each
# time a rewrite is issued. So the 'fr/accueil/' I'm redirecting to
# will itself be rewritten as /accueil/index.php?lang=fr. That is
# why I'm providing 3 conditions:
# If URI does not start with 'en' AND
# If URI does not start with 'fr' AND
# If QUERY_STRING is not exactly 'lang'
RewriteCond %{REQUEST_URI} !^en/.*$
RewriteCond %{REQUEST_URI} !^fr/.*$
RewriteCond %{QUERY_STRING} !^lang$
RewriteRule ^.*$ fr/accueil/ [NC,R=301]
Please put me out of my misery. Cheers!
try replacing the last line
RewriteRule ^.*$ fr/accueil/ [NC,R=301]
With
RewriteRule ^.*$ fr/accueil/ [NCL,R=301]