.htaccess: rewrite .htm urls internally to .php, but also redirect .php urls to .htm - apache

I have a php site. For all of the page links I use foo.htm, and internally rewrite this to foo.php with .htaccess:
RewriteRule ^(.*)\.htm$ $1.php [NC,L]
This works great - however, it still allows you to use the foo.php url. I would like to 301 redirect foo.php to foo.htm to prevent any old foo.php search engine results from hanging around and rewrite the foo.htm url internally to foo.php
I can't figure out how to do this without creating a loop.

Put this code in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s(.+)\.php [NC]
RewriteRule ^ %1.htm [R=301,L]
RewriteRule ^(.*)\.htm$ /$1.php [L,NC]

RewriteRule ^(.*)\.htm$ $1.php [NC,L]
RewriteRule ^(.*)\.php$ $1.htm [NC,R]
the option L stops the rewriting engine, preventing a loop.

Related

the right way redirect url using .htaccess

what is the right way to redirect page using .htaccess?
for ex i want to redirect myweb.com/dorama/901110204/todome-no-kiss--paralel to myweb.com/dorama/1101071510/todome-no-kiss--parallel
but when i try to access old link, link direct to myweb.com/dorama/1101071510/todome-no-kiss--parallel?cat=dorama&idp=901110204&post=todome-no-kiss--paralel
my full .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#Redirect Dead Link
Redirect 301 /dorama/901110204/todome-no-kiss--paralel http://www.piratefiles.org/dorama/1101071510/todome-no-kiss--parallel
Redirect 301 /dorama/601181905/life-as-a-girl http://www.piratefiles.org/dorama/1101072233/life-as-a-girl
Redirect 301 /dorama/801130021/todome-no-kiss http://www.piratefiles.org/dorama/1101071405/todome-no-kiss
RewriteRule ^([A-Za-z]+)/([0-9]+)/([\w-]+)/?$ view.php?cat=$1&idp=$2&post=$3&%{QUERY_STRING} [NC,L]
RewriteRule ^category/([\w-]+)/?$ category.php?cat=$1&%{QUERY_STRING} [NC,L]
# To externally redirect /dir/abc.php to /dir/abc
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [QSA,NC,L]
</IfModule>
ErrorDocument 404 http://www.piratefiles.org/404.php
RewriteRule directives "run" before Redirect ones do. Thus, if you want to force the redirections to run in a particular order, you need to convert them all to Rewrite directives.
Replace the three Redirect directives with:
RewriteRule ^dorama/(\d+)/(todome-no-kiss--paralel|life-as-a-girl|todome-no-kiss)$ http://www.piratefiles.org/$1/$2 [R=301]
Tweak as necessary, but that's the reason it's failing, and a proposed solution.
(Also, standard note here: If you have access to your main server config, .htaccess files should be avoided whenever possible. .htaccess files are for people that don't have access to the main config.)

.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 not working on ip/~username

Here is my case. I'm trying to use .htaccess for rewrite url, but no matter how I try, browser always show 500 error or 404 error.
here is my code.
RewriteEngine On
RewriteBase /~torinots/beta/
RewriteCond %{REQUEST_FILENAME} !^(/beta/home)
RewriteRule ^home$ index.php [L]
example path: http://xx.xx.xx.xx/~username/beta/
Pls advice.
Update
I found this work!
RewriteEngine On
RewriteBase /~torinots
RewriteRule ^beta/home/?$ beta/index.php [L,NC]
I assume you have the .htaccess file in /~username/beta, since you use paths relative to that in the example above. Having a simple rule like the following rule will correctly internally rewrite the url, assuming that no other rules in .htaccess files higher up are interfering.
RewriteRule ^home$ index.php [L]
If you want to redirect a request to index.php too, then you need to prevent an infinite loop from happening. You can either use the END-flag (version 2.3.9 and up) or the THE_REQUEST trick.
#internal rewrite, and then stop everything
RewriteRule ^home$ index.php [END]
#Rewritebase is possibly needed for the redirect
RewriteBase /~torinots/beta/
#external redirect
RewriteRule ^index\.php$ home [R=301,L]
or:
#internal rewrite, and then stop everything
RewriteRule ^home$ index.php [L]
#Rewritebase is possibly needed for the redirect
RewriteBase /~torinots/beta/
#external redirect
RewriteCond %{THE_REQUEST} index\.php
RewriteRule ^index\.php$ home [R=301,L]

.htaccess - Redirect to a subfolder according to the preferred language

we have a website in English. Now we want to have a translated copy of this website in German.
The index.html is located in the root folder: www.mydomain.com/index.html
Now I want to create a htaccess file which redirects all user which have set the preferred language in their browser to German (de, de-de, de-at, de-ch) to www.mydomain.com/de/index.html. All other user should be redirected to www.mydomain.com/en/index.html.
I tried a lot but I can't make it work. I do not really understand the regular expression thing and it's usage.
Most of the tries ended up with error 403 or 500. The current htaccess doesn't end up in an error, but it doesn't work.
The htaccess is now:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^/(.*)$ /de/$1 [L]
RewriteRule ^/(.*)$ /en/$1 [L]
It seems like the file doesn't work at all. When I open www.mydomain.com it opens www.mydomain.com/index.html
Htaccess and rewriting works in general:
RewriteRule ^(.*)$ http://www.google.com/ [R=301,L]
works.
If I delete the slash behind ^
RewriteRule ^(.*)$ /en/$1 [L]
I get an error 500.
What do I do wrong?
I actually want the server to redirect to the webpage which the user tried to open. So if the user opens mydomain.com/index2.html he should be redirected to mydomain.com/xx/index2.html (and not to index.html)
Any ideas?
Thanks,
Jaroslaw
You can use this rule:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteRule ^(en|de)/ - [L,NC]
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^(.*)$ /de/$1 [L]
RewriteRule ^(.*)$ /en/$1 [L]

mod_rewrite keeps adding .html for files

I have a redirect situation where the site is part dynamic and part generated .html files.
For example, mysite.com/homepage and mysite.com/products/42 are actually static html files
Whereas other URLs are dynamically generated, like mysite.com/cart
Both mysite.com and www.mysite.com are pointing to the same place. However I want to redirect all of the traffic from mysite.com to www.mysite.com.
I'm so close but I'm running into an issue where Apache is adding .html to the end of my URLs for anything where a static .html file exists - which I don't want.
I want to redirect this:
http://mysite.com/products/42
To this:
http://www.mysite.com/products/42
But Apache is making it this, instead (because 42.html is an actual html file):
http://www.mysite.com/products/42.html
I don't want that - I want it to redirect to www.mysite.com/products/42
Here's what I started with:
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]
I tried making the parameters and the .html optional, but the .html is still getting added on the redirect:
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)?(\.html)?$ http://www.mysite.com/$1 [R=301,L]
What am I doing wrong? Really appreciate it :)
Here is the code you will need in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## first add www to your domain for and hide .html extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ http://www.mysite.com%1 [R=301,L]
## add www to your domain for other URIs without .html extension
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^ http://www.mysite.com%{REQUEST_URI} [R=301,L]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f [NC]
RewriteRule ^ %{REQUEST_URI}.html [L]
Maybe you should try looking at apache's mod_negotiation to get rid of the .html or any file extension?
Link: http://httpd.apache.org/docs/2.0/mod/mod_negotiation.html