original url is one of the following:
example.com
example.com/index.php
example.com/index.php?c=lorem
if example.com - nothing should be done
if example.com/index.php - it should be visible as example.com and here is my code - works fine:
RewriteEngine ON
RewriteRule ^index\.php/?$ https://%{HTTP_HOST}/ [NC,R=301]
RewriteRule ^/?$ index.php [L]
if example.com/index.php?c=lorem it should be visible as example.com/lorem
and lorem should be accessible by php in both cases:
if a user type example.com/index.php?c=lorem or
a user type example.com/lorem
pls help
Could you please try following, based on your shown samples.
Please do clear your browser cache before testing your URLs.
RewriteEngine ON
##This rule is for handling index.php file in uri.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php/?$ / [R=301,NC,L]
##This rule is to redirect from index.php?c=lorem to lorem in uri.
RewriteCond %{THE_REQUEST} index\.php\?c=(lorem)\s [NC]
RewriteRule ^ http://%{HTTP_HOST}/%1? [R=301,L,NE]
##Rule for non-existing files/directories to index.php with variables.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?c=$1 [L]
OR: Above is for specifically lorem in case your query string could be anything then try following.
RewriteEngine ON
##This rule is for handling index.php file in uri.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php/?$ / [R=301,NC,L]
##This rule is to redirect from index.php?c=lorem to lorem in uri.
RewriteCond %{THE_REQUEST} index\.php\?c=([^\s&]*) [NC]
RewriteRule ^ http://%{HTTP_HOST}/%1? [R=301,L,NE]
##Rule for non-existing files/directories to index.php with variables.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?c=$1 [L]
You may use these rules in your site root .htaccess:
DirectoryIndex index.php
RewriteEngine On
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php\s [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+index\.php\?c=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ index.php?c=$1 [L,QSA]
Related
I have the following code in my htaccess file which is supposed to redirect all www requests to the non www version of index.html, but its not working and I'm not sure why. Can anyone help?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
Have it like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^index\.html$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
Here we are capturing value after www. in a capture group and then using that as %1 in target URL.
Make sure to clear your browser cache or use a new browser before you test this change.
.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
With this setup the page /foo/bar is accessible under the pretty URL example.com/foo/bar while the ugly URL example.com/index.php/foo/bar is still valid. What I need to achieve is a permanent redirection from example.com/index.php/foo/bar to example.com/foo/bar.
RewriteRule .* index.php [R=301,L] doesn't work.
RewriteRule (.*) index.php/$1 [R=301,L] does the exact opposite, it redirects from example.com/foo/bar to example.com/index.php/foo/bar. Please help me out!
I recommend writing this:
RewriteEngine On
RewriteBase /
# remove index.php from / OR any /dir
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
# redirect rule to remove /index.php/ from start
RewriteRule ^index\.php(/.*) $1 [L,R=301,NC]
RewriteCond %{REQUEST_URI} !^/index\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
In the new rule, we match a pattern using regex ^index\.php(/.*) which matches a URI that starts with /index.php/ followed by zero or more of any characters. (/.*) is our match and the capture group after /index.php which is used as back-reference i.e. $1 (string that we've captured in group #1) in target to get a URI after /index.php as desired.
References:
Apache mod_rewrite Introduction
.htaccess tips and tricks
here is my code for hide .html and .php extension when user view my site, http://example.com/xxx.html to http://example.com/xxx
But I hope to make user when they input http://example.com/xxx.html, they cannnot access and jump to 404 not found.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# Remove .html-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)/$ $1.html
# End of Apache Rewrite Rules
</IfModule>
Anyone have solution?
You can add this as your first rule:
RewriteCond %{THE_REQUEST} \.(?:html|php)\s [NC]
RewriteRule ^ - [R=404,L]
Update
Perhaps this will fix your ErrorDocument problem:
RewriteCond %{THE_REQUEST} \.(?:html|php)\s [NC]
RewriteCond %{REQUEST_URI} !=/error404.html
RewriteRule ^ - [R=404,L]
I am trying to rewrite my urls to another file if they do not already exist. I have an .htaccess file in the root of my site that looks like this.
RewriteEngine on
# Add slash to end of request
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
# API redirect
RewriteCond %{REQUEST_URI} ^/api
RewriteRule . api/v1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^ secondary\.
RewriteRule . sites/secondary [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sites/main.php [L]
My intention is to serve two sites from the same folder and use the rewrites to point to the appropriate routers. The api redirect works great. However, the main and secondary redirect do not work unless I specify a route other than example.com (example.com/test/ correctly rewrites).
Any insight is greatly appreciated.
Use:
RewriteEngine on
# Add slash to end of request
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule ^ %{REQUEST_URI}/ [R=301,L]
# API redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^api api/v1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^ secondary\.
RewriteRule ^ sites/secondary [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ sites/main.php [L]
Because . is at least equal to one character.
I have the following code in my .htacess but it didn't work right. Is it because mod-rewrite is no "on", if so, how can i check?
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\$ $1.php [nc]
I wanted to rename my address, example:
http://www.abc.com -> http://www.abc.com
http://abc.com -> http://www.abc.com
http://www.abc.com/123.html -> http://www.abc.com/123
http://www.abc.com/12-12-12.html -> http://www.abc.com/12-12-12
http://subdomain.abc.com/123.html -> http://subdomain.abc.com/123
Basically removing the extension and ensuring that its www is intact.
Edited:
It was rewrote into
Options +FollowSymlinks
RewriteEngine on
RewriteCond % ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^(.*).php $1
but still not working
Step 1.
Change all of your links to have the .html removed from links. Make sure you have Multiviews turned off:
Options -Multiviews
Step 2.
You need rules to redirect the browser when there is a www missing from the hostname:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Step 3.
You need rules to redirect the browser when a URL is requested with .html or .php with the extensions removed:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\ ]+)\.(php|html?)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ /%2 [L,R=301]
Step 4.
You need rules to internally rewrite the URI if the request was actually for a php or html file:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^(.*)$ /$1.htm [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [L]
.htaccess
So all in all, you should have these rules in your htaccess (ditch any rules you may already have trying to solve this issue):
# Make sure Multiviews is off
Options -Multiviews
# turn on rewrite engine
RewriteEngine On
# Redirect requests that are missing www and not a subdomain
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# Redirect if requests is for a .htm, .html, or .php
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\ ]+)\.(php|html?)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ /%2 [L,R=301]
# rewrite to the proper extension if such a file exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^(.*)$ /$1.htm [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [L]
So altogether:
if browser URL address bar says: http://example.com/index.html it will be redirected to http://www.example.com/index
if browser URL address bar says: http://www.example.com/index it will be shown the content at http://www.example.com/index.html (or index.php if that exists). Address bar remains unchanged.
if browser URL address bar says: http://foo.example.com/images/image.png you will see the image at /images/image.png. Address bar remains unchanged.
if browser URL address bar says: http://foo.example.com/path/foo/bar.php it will be redirected to http://foo.example.com/path/foo/bar
if browser URL address bar says: http://foo.example.com/path/foo/bar it will be shown the content at http://foo.example.com/path/foo/bar.php. Address bar remains unchanged.
Remove .html:
RewriteRule ^(.*).html $1 [L,QSA]
Add www.:
RewriteCond % ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
On which hosting is located your website ? Are you sure mod-rewrite is enabled ? Did you contact your host to put it on, if not activated ?
Solution previously posted by androbin (https://stackoverflow.com/a/12553613/1713771) is correct.
The .htaccess file that I use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
</IfModule>
Which removes the .html file extension and forces www.