Hi I am making a site where I want nice and clean links. I am trying to make a link with a get parameter look alot cleaner. Here is a sample link of how the link is at the moment:
http://www.example.com/index.php?item=cd-player
Here is how I want the link to look:
http://www.example.com/cd-player
I have been able to get rid of index.php from the url to leave just the parameter but I need help in getting rid of the "?item" bit as well.
Here is what I have tried so far:
RewriteRule ^index\.php(.*)$ /$1 [R,L,QSA]
RewriteRule ^(.+?)/?$ /?item=$1 [L,QSA]
Also, this may be a php code rather than a code for the .htaccess file, but if a user enters http://www.example.com/cd-player, how will it know that "cd-player" is the value of a get variable called "item"?
EDIT: The problem I am having with the answers below is that I am using the following code to remove file extensions and the code in the answers below treats the files as a query:
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Try this instead :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?item=$1 [L]
You need 2 rules actually:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?item=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
# add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?item=$1 [L,QSA]
You need to do it like this. This will allow you to have clean links
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/.]+)$ $1.php [NC,L]
#This means if its not a real file
RewriteCond %{REQUEST_FILENAME} !-f
#This means if its not a real directory
RewriteCond %{REQUEST_FILENAME} !-d
#Then take the capture and rewrite it
RewriteRule ^(.+)$ /index.php?item=$1 [L]
Related
It is a very common question on the internet but unfortunately, I didn't find any solution yet. My issue is, I already using below codes, which is working fine for article purpose (site.com/my-first-article).
#remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#for pretty url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ blog.php?qTitle=$1 [NC,L]
But now i need (site.com/profile/user-name), when I copy and edit the code above for user profile it doesn't work.
#remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#for pretty url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ profile.php?username=$1 [NC,L]
I hope I defined the question well and some can understand my issue. Thanks for your time.
My output
To change the address site.com/profile.php?username=J0R1AN to site.com/profile/J0R1AN for example, you would need to use this code:
RewriteEngine On
# If user directly accesses /profile/[username]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?profile/(.*?)/?$ /profile.php?username=$1 [L]
# If user accesses /profile.php?username=[username]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /profile\.php\?username=([^\&\ ]+)
RewriteRule ^/?profile\.php$ /profile/%1? [L,R=301]
You can even keep using $_GET in the profile.php file
Source: https://www.taniarascia.com/rewrite-query-string-to-path-with-htaccess/
Hello i want to convert php urls into SEO urls every thing is working fine at my end but the files in the root doesn't work here is the code which i am using in htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ show_staff.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ show_staff.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)$ show_users.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ show_users.php?url=$1
but if i add these lines right after the above lines the files which are in root doenst work without .php extention here is what i want to add here i want to mention without or with using below code the above code works fine for me
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* $0.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
It looks like your stuff is all in the wrong order. Some things:
If you don't have an L flag for a rewrite, the rules after will continue to process the rewritten request. So you want to use the L flag.
Rewrite conditions only apply to the immediately following rule, so you've got a condition that's just dangling somewhere and not being used.
The show_users.php rules will never work the way you've set things up, the regex pattern is the same, so no matter what, your requests will always go to show_staff.php.
Your rules aren't in the right order, two things need to happen if I'm guessing what you're trying to do. You need to match against a request with a php extension and externally redirect the browser, then you need to internally rewrite that request back to the URI with a php extension.
So something like:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \ /+([^/]+)\.php
RewriteRule ^ /%1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
ReweriteRule ^ %{REQUEST_URI}.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9-/]+)/?$ show_staff.php?url=$1 [L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
## hide .php extension
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9/-]+)$ show_staff.php?url=$1 [L,QSA]
I am working on an .htaccess-file lying in mysite.com/dir/.htaccess that redirects mysite.com/dir/page to mysite.com/dir/page.php if possible and redirect to mysite.com/dir/ if not possible. So any "wrong" request will be redirected to the main page. The code im using is:
RewriteEngine on
# determine DIR_BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=DIR_BASE:%1]
# see if .php is found
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php
# if not found redirect to %{ENV:DIR_BASE}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:DIR_BASE} [R]
This all works well. (By the way: i want to determine the base-dir of the file, because i maybe want to change the directory name later, but not change the file). What i now want is to block some directories for the user, because they contain php-libraries that should not be accessible. how can i e.g. block the directories mysite.com/dir/lib/* and mysite.com/dir/lib2/* and redirect them to mysite.com/dir/ again like i did before? I also want to use code not like
RewriteRule ^lib/(.)* %{ENV:DIR_BASE}
that would redirect mysite.com/dir/lib/../page to the main page instead of mysite.com/dir/page where it should belong. i tried very much with %{REQUEST_FILENAME} and %{REQUEST_URI}, but i am only a beginner when it comes to mod_rewrite. do you know a solution?
This should work:
RewriteEngine on
# determine DIR_BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=DIR_BASE:%1]
RewriteRule ^lib2?(/|$) %{ENV:DIR_BASE} [L,NC,R]
# see if .php is found
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.*)$ $1.php [L]
# if not found redirect to %{ENV:DIR_BASE}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:DIR_BASE} [L,R]
What I am trying to do it simple:
I want to remove .php extension from file for which I am using following code and it is working
#for hiding.php extension
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
############################-------- END ---------########################
Also I want to show a $_GET parameter on screen for which the link should be link this
localhost/123 which is representation of localhost/somepage.php?para=123
and for which I am using following code which is also working fine
#for parameter Display
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ somepage.php?para=$1
############################-------- END ---------########################
Problem: These codes are working fine when not written together in same `.htaccess` file. But when written together and When I have to access login page using localhost/login. .htaccess treat login as get parameter value. Any solution to this problem?
Try this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.+)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ somepage.php?para=$1 [L,QSA]
i used the htacess to remove the php extension and it worked but i also have a page called profile wich i shorten but that make the pages names being taken as a a profile user name, is there a way to fix this?
the results i want is like this:
localhost/path_folder/index.php to localhost/path_folder/index
and
localhost/path_folder/profile?username=name to localhost/path_folder/name
and my htaccess code for the removing extension is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]
and the code for the profile page is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /path_folder/profile.php?username=$1
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
## First try To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /path_folder/$1.php [L]
## if PHP file not found then load profile page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /path_folder/profile.php?username=$1 [L,QSA]
If you want to remove file extension
RewriteEngine On
RewriteRule ^path_folder/([^/]*)$ path_folder/$1.php [L]
Shorten URL for userprofiles
RewriteEngine On
RewriteRule ^path_folder/profile/([^/]*)$ path_folder/profile?username=$1 [L]