I want to remove a file name from url, where url is: mysite.com/project.php?page=AAA and here want to remove project.php?page=. So previously I removed successfully another file name from url by below method where url was: mysite.com/userinfo.php?user=111 and removed userinfo.php?user=
htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ userinfo.php?user=$1 [L,QSA]
But now when I am going apply same method for remove project.php?page=. Its not work.
I tried:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ project.php?page=$1 [L,QSA]
Here is all htaccess
#Remove php#
RewriteEngine On
RewriteBase /
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1%2 [R=302,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
#Remove Userinfo#
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ userinfo.php?user=$1 [L,QSA]
To redirect from mysite.com/userinfo.php?user=111 to mysite.com/111 it is enough 2 lines:
RewriteCond %{QUERY_STRING} user=([^?&]+) [NC]
RewriteRule ^userinfo.php$ /%1? [R,L]
Related
I am getting errors when I am trying to rewrite URL.
From this:
www.example.com/products/product-details.php?alias=abc-def-123
To this:
www.example.com/products/abc-def-123
Here is my .htaccess file
#Turn Rewrite Engine On
Options +FollowSymLinks -MultiViews
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([a-zA-Z0-9-]+)$ products/product-details.php?alias=$1 [NC,L]
</IfModule>
I am hitting this URL:
www.example.com/products/abc-def-123
Update:
With your shown samples please try following rules. Please make sure to keep your htaccess in your root folder along with your products folder(not inside it).
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)/?$ https://example.com/$1 [L,NE,R=301]
RewriteCond %{THE_REQUEST} \s/products/.*\?alias=(\S+)\s
RewriteRule ^ /products/%1? [R=301,L]
RewriteRule ^products/(.*)/?$ products/product-details.php?alias=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f [NC]
RewriteRule ^(.*)/?$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f [NC]
RewriteRule ^(.*)/?$ $1.php [NC,L]
</IfModule>
Yeah!!!!!!!
I solved my problem myself and also I want to thanks #RavinderSingh13 to make effort.
Here is the whole .htaccess file
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)/?$ https://example.com/$1 [L,NE,R=301]
RewriteRule ^products/([^/\.]+)?$ products/product-details.php?alias=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f [NC]
RewriteRule ^(.*)/?$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f [NC]
RewriteRule ^(.*)/?$ $1.php [NC,L]
</IfModule>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d # not an existing dir
RewriteCond %{REQUEST_FILENAME} !-f # not an existing file
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f # and page.php exists
# redirect to the physical page
RewriteRule ^(.*)$ $1.php [L]
# otherwise, redirect to serve.php
RewriteRule ^ /serve.php [L]
RewriteRule ^(\w+)$ ./serve.php?id=$1
So in the first part of the code I just turn http into https. My aim is that I can use urls without an .php but in my old code I had the problem that if I did so, it was used as an id for my serve.php page. So if use https://example.com/contact it was like https://example.com/serve.php?id=contact but I want it to work as https://example.com/contact.php but on the other side I want that ids that arent directions or file should still work as ids. My old code was...
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(\w+)$ ./serve.php?id=$1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You can use these rules in your site root .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# skip rules below this for files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# rewrite to the physical php page
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# otherwise, redirect to serve.php
RewriteRule ^/?$ serve.php [L]
RewriteRule ^(\w+)/?$ serve.php?id=$1 [L,QSA]
I have the following code and I am trying to do something like that. Let's say I have an address - mydomain.com/server2/ when I type anything after slash for example mydomain.com/server2/whatever I want to load server2.php but address has to be the same
<IfModule mod_rewrite.c>
Options +FollowSymlinks
# Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
# remove index.php
RewriteRule ^index\.php/?$ / [L,R=301,NC]
# Hide File Extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
# Add 301 redirects to new extensionless file
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.mydomain.com/$1 [R=301,L]
# Add the trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://www.mydomain.com/$1/ [R=301,L]
</IfModule>
also I don't know if above code is 100% correct. Thanks for any help.
There are problems in your code and in certain URLs it will give you infinite looping. Replace your code with this:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=301,L,NE]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
# Add the trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
</IfModule>
I just want to hide the dir name from URL.
From: example.com/dirname/somepage
To: example.com/somepage
That code doesn't work for me, I have probably made some mistakes
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule / /dir/$1 [L]
I have already this in .htaccess (to hide php extension)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
This should be your complete .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+dirname/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^dirname/)^(.*)$ /dirname/$1 [L,NC]
how to add / at the end using following code after removing html extension:
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]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
You will need to make some changes.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
#Code to add forward slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(?:\.\w+|/)$
RewriteRule (.*) $1/ [R,L]
#To check whether a .html appended string is a file existing on the system
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule (.*)/ $1.html [L]
#### NOT REQUIRED
#RewriteCond %{SCRIPT_FILENAME}/ -d
#RewriteCond %{SCRIPT_FILENAME}.html !-f
#RewriteRule [^/]$ %{REQUEST_URI}/ [R,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R,L]
Also these rules will not work as desired:
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]+/$ %{REQUEST_URI}.html [QSA,L]
cos, %{REQUEST_URI} will always have / at the end. If a URI like domain.com/about is requested for,
It will be rewritten to:
domain.com/about/
and finally to
domain.com/about/.html