Url renaming using .htaccess file - apache

Most are for redirections and removing file extensions or for dynamic urls.
The problem being I can't get a simple static url to rename
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^fileThathasalongname file.html
What I currently have 'mysite.co.uk/fileThathasalongname.html'
What I want is 'mysite.co.uk/file/' while file = file.html
using:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^FileIWantToChange.html FriendlyNamed.html
Using this gives me the error multiple Choices
+++++++++++++++++Edit+++++++++++++++++++++++++
Thought i'd add my final version for people to look at, it may help, the anwser below is correct.
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteRule ^FriendlyNamed.html FileIWantToChange.html [L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
RewriteCond %{HTTP_HOST} ^www.mysire.co.uk [NC]
RewriteRule ^(.*)$ http://mysite.co.uk/$1 [L,R=301]
all works a charm!

I see multiple issues going on. Firstly the regular expression is matched against the friendly URL the user types in. So you need to swap your friendly url and file path with each other. The friendly or "fake" name goes on the left while the url to redirect to silently goes on the right. Also make sure you've set the directory base to /. Finally it's good to add an [L] to enforce it to be the last rule in case anything in the same file tries to rewrite the path. Due note that other htaccess files lower down, depending on the files location, will also be checked even when enforcing the rule has the last rule. Also junk the options part completely. Give this a try:
RewriteBase /
RewriteEngine On
RewriteRule ^FriendlyNamed.html FileIWantToChange.html [L]

RewriteRule ^fileThathasalongname.html file.html

Related

.htaccess rewrite - filename.html to /?c=filename

I need it to work with all html URLs (except index), I tried using .htaccess with no luck.
For example... change this link from: http://www.example.com/signup.html to http://www.example.com/?c=signup.
Also if I enter directly to http://www.example.com/?c=signup, it must mirror http://www.example.com/signup.html.
EDIT
I found the way to do that, except for the case that I enter directly to the .html file, in that case, it does not rewrite.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{QUERY_STRING} ^c=([^&]+) [NC]
RewriteRule ^/?$ %1.html [NC,L,QSA]
RewriteRule ^/?$ %1.html [NC,L,QSA]
To redirect (presumably you meant "redirect", not "rewrite") from signup.html to /?c=signup then you can do something like the following before your existing rewrite:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ([^.]+)\.html$ /?c=$1 [R,L]
The condition that checks against the REDIRECT_STATUS environment variable prevents a redirect loop when your existing directive rewrites to the .html file.
Note that this is a temporary (302) redirect. Change the R to R=301 if this is intended to be permanent, but only once you have confirmed it's working OK.
Your existing directives could be tidied, in summary:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Redirect from signup.html to /?c=signup
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ([^.]+)\.html$ /?c=$1 [R,L]
# Rewrite from /?c=signup to signup.html
RewriteCond %{QUERY_STRING} ^c=([^&]+)
RewriteRule ^/?$ %1.html [QSD,L]
Presumably, you want to discard the c= query string from the rewritten substitution?

Apache - Changling browser URL address with .htaccess

I tried to change the browser url from localhost:8888/dev/test.php to localhost:8888/dev/testRewrite/ following the example of this question on S.O.
At first, I tried the provided code in that question
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^testRewrite/$ test.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php [NC]
RewriteRule ^test.php$ /testRewrite/ [L,R=301]
and managed to change localhost:8888/test.php to localhost/testRewrite/
But applying the same to the sub-directory /dev/ resulted in no change.
I tried adding RewriteBase /dev/, moving the .htaccess to the sub-directory and even clearing the cache but it made no difference and the url remained http://localhost:8888/dev/test.php. So I think I have not understood the rewrite rule properly and am doing it the wrong way.
So what is the right way of doing this? Also does the .htaccess need to be in the sub-directory or it can be in the root directory?
After a little bit more tinkering and searching, I finally got what I needed
RewriteEngine on
RewriteCond %{THE_REQUEST} (\S*?)/test\.php [NC]
RewriteRule ^ %1/test/ [L,R=301]
RewriteRule ^(.*)/test/ /$1/test.php [NC,L]
Regex substitution was the solution to my requirements
Considering your code
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^testRewrite/$ test.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php [NC]
RewriteRule ^test.php$ /testRewrite/ [L,R=301]
The problem lies in the last line.
^test.php$
In Regex ^ symbolize the start of text and $ the end of text.
Thus ^test.php$ has only one exact match. "test.php" But what you to redirect is "/dev/test.php".
Thus your last line needs to be:
RewriteRule test.php$ /testRewrite/ [L,R=301]
If you want to be very exact, you could also make it
RewriteRule ^/dev/test.php$ /dev/testRewrite/ [L,R=301]
You might need to do the same in line 3.

.htaccess URL rewrite not working, tried all possible options

I was trying to rewrite a URL for making my site SEO friendly, but .htaccess rewrite not seems to work.
My URL is
www.tasteofkochi.com/dine-detail.php?a=150
I need this to look like
www.tasteofkochi.com/sometext/150
I did the simple formula but it's not reflecting, intact nothing happens at all. If I add some junk char in htaccess, site crashes, which means htaccess is working fine. Also I added a formula to remove .php extn, and that too works fine. Only issue is with this one. Can anyone please help me. I enable rewrite in httpd and allow all in directories, still not working.
Below is my .htacces
RewriteEngine on
Options +FollowSymLinks -MultiViews
RewriteBase /
## hide .php extension
# To externally redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteRule ^detail/([0-9]+)/?$ dine-detail.php?a=$1 [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?p=$1 [L,QSA]
We can create pretty urls with .htaccess and php by mainly two files one is .htaccess and another index.php
Example

.htaccess URL and directory redirect

This is my first post so excuse me if something it's wrong with my way of asking for help. I've been reading other posts related to my problem but none solved it.
My path:
http://example.com/m/page1
http://example.com/m/page2
...
And so on.
I want to change the /m/pageX to /m/#pageX without hardcoding the URL and without using any additional php scripts.
At the first sight, I've accomplished this task by writing the following .htaccess configuration file:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^[dm]/(.+)$ /m/#$1 [R=302,NE,L,NC]
## hide .php extension
RewriteCond %{THE_REQUEST} \s/+common_([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
## To internally forward
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/common_$1.php -f
RewriteRule ^(.+?)/?$ common_$1.php [L]
Is my code the best solution or there is other way to approach my task ?
How can I redirect the /m/pageX to /d/#pageX ?
Thank you
The # sign is not supported in URLs.
This will rewrite /m/pageX to /d/pageX:
RewriteEngine On
RewriteRule ^m/(.*) /d/$1 [L,R=301]
This will rewrite /m/pageX to /d/pageX#example, but #example is fix:
RewriteEngine On
RewriteRule ^m/(.*) /d/$1#example [NE,L,R=301]
[NE] is necessary to keep the hash (#) sign.
This will rewrite /m/pageX to /d/#pageX:
RewriteEngine On
RewriteRule ^m/(.*) /d/#$1 [NE,L,R=301]
Try these rules in your root .htaccess (a level above /m/ and /d/):
RewriteEngine On
RewriteRule ^d/(.+)$ /m/#$1 [NE,NC,L,R=302]
RewriteRule ^(.+)$ /#$1 [NE,L,R=302]
Make sure to remove any /d/.htaccess file.
If for some reason you want to do it from /d/.htaccess then use this rule in /d/.htaccess file:
RewriteEngine On
RewriteRule ^(.+)$ /m/#$1 [NE,NC,L,R=302]

Apache2 mod_rewrite pattern restriction?

So I have a site that I want to make SEO friendly by using mod_rewrite. I want to make the URLs easy to remember by dropping the .php on the end of them and using mod_rewrite to re-attach them later on. So for example say http://example.com/about would point to http://example.com/about.php. I have the RewriteRule that should work from my experience but for some reason doesn't.
My rule:
RewriteEngine On
RewriteRule ^/?about$ about.php [L]
RewriteRule ^/?faq$ faq.php [L]
Now these rules don't work like that exactly. it seems that if I rename the files to blah.about.php and blah.faq.php and change the RewriteRule lines to reflect the new filenames it works.
Is this a restriction of mod_rewrite where the Pattern can't be so close to the target file?
Try this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*)\.php [NC]
RewriteRule ^ /%1 [R=301,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
First rule redirect the request to phpless page and the next internally to the php file itself.
Is this a restriction of mod_rewrite where the Pattern can't be so close to the target file?
Its not really a restriction, you just can't redirect the php file to non-php and then back to it as it generates a loop, so what we do is capture the request and redirect from there and then internally redirect to the file.
Unless of course like you have seen they have a different naming.