Remove a specific file name using htaccess - php

I have not enough experience in writing rules in .htaccess.
My site url is like
http://exampletest.com/detail.php?i=my-url
I want to rewrite that url to
http://exampletest.com/my-url
What I have tried so far:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} /detail\.php\?i=([^\s&][A-Za-z0-9-]+) [NC]
RewriteRule ^ detail %1? [R=302,L]
That makes me able to access my site using:
http://exampletest.com/detail/my-url
But I want to omit detail from url as well.

You should redirect internally as well so your code should looks like this :
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} /detail\.php\?i=([^\s&][A-Za-z0-9-]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /detail.php?i=$1 [L]

Try this:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} /detail\.php\?i=([^\s&][A-Za-z0-9-]+) [NC]
RewriteRule ^ %1? [R=302,L]

Related

Removing a word from our urls

We have the following file structure in our ftp account.
/index.php
/views/account/users/index.php
/views/account/clients/index.php
/views/profile/index.php
/views/settings/index.php
....
Which means that the urls is as follows:
www.thesite.com/
www.thesite.com/views/account/users/
www.thesite.com/views/account/clients/
www.thesite.com/views/profile/
www.thesite.com/views/settings/
...
What we need, is to remove views from all possible variations, so that we can link to, or enter the following url in the browser's address bar.
www.thesite.com/account/users/
Any help would be greatly appreciated.
Try these rules in your site root .htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+views/(\S*)\s [NC]
RewriteRule ^ /%1 [NE,L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!views/).+)$ views/$1 [NC,L]
You can use the following Rules in Root/.htaccess :
RewriteEngine on
#/foo to /views/foo
RewriteCond %{THE_REQUEST} /views/([^/.\s]+) [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteRule ^([^/.]+)/?$ /views/$1 [NC,L]
#Rewrite /foo/bar to /views/foo/bar
RewriteCond %{THE_REQUEST} /views/([^/]+)/([^/.\s]+) [NC]
RewriteRule ^ /%1/%2 [NC,L,R]
RewriteRule ^([^/]+)/([^/.]+)/?$ /views/$1/$2 [NC,

Redirect to a subfolder using htaccess

I have searched all the similar topics in the forum and nothing helped me. Here is what I am looking for:
I have a site having a login url as
site.com/folder1/php/login.php
Now I want to hide the "php" folder name from url, also remove .php extension so that it will look like
site.com/folder1/login
Also the folder name "folder1" may vary, but the folder name "php" remains the same in all urls.
Try this in your Root/.htaccess
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/?([^/]+)/([^/]+)/([^.]+)\.php [NC]
RewriteRule ^ /%1/%3 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /folder1/php/$2.php [NC,L]
This should be working.
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/+php/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^php/)^(.*)$ /php/$1 [L,NC]

how to rewrite multiple get variables using htaccess

i have a page with search form(fields: make,model,year,mileage,price)
on form submit url is:
http://www.example.com/buy-car.php?make=xxx&model=xxx&mileage=100;10000&year=2000;2015&price=20000;60000&sort=date;DESC
i want to rewrite to look like:
http://www.example.com/buy-car/xxx/xxx/100;10000/2000;2015/20000;60000/date;DESC
Note: query string may or may not contain all the variable
i tried multiple rules but still not getting what i want
RewriteCond %{THE_REQUEST} /buy-car\.php\?make=([^\s&]+)&model=([^\s&]+)&year=([^\s&]+)\&mileage=([^\s&]+)\&price=([^\s&]+)\s [NC]
RewriteRule ^ buy-car/%1/%2/%3/%4/%5? [R=302,L]
RewriteRule ^buy-car/([\w-]+)/([\w-]+)/([\^;a-zA-Z0-9_-]+)/([\^;a-zA-Z0-9_-]+)/([\^;0-9-]+)/?$ buy-car.php?make=$1&model=$2&year=$3&mileage=$4&price=$5 [L,QSA,NC]
RewriteCond %{THE_REQUEST} /buy-car\.php\?make=([^\s&]+)&model=([^\s&]+)&year=([^\s&]+)\&mileage=([^\s&]+)\s [NC]
RewriteRule ^ buy-car/%1/%2/%3? [R=302,L]
RewriteRule ^buy-car/([\w-]+)/([\w-]+)/([\^;a-zA-Z0-9_-]+)/([\^;a-zA-Z0-9_-]+)/?$ buy-car.php?make=$1&model=$2&year=$3&mileage=$4 [L,QSA,NC]
RewriteCond %{THE_REQUEST} /buy-car\.php\?make=([^\s&]+)&model=([^\s&]+)&year=([^\s&]+)\s [NC]
RewriteRule ^ buy-car/%1/%2/%3? [R=302,L]
RewriteRule ^buy-car/([\w-]+)/([\w-]+)/([\^;a-zA-Z0-9_-]+)/?$ buy-car.php?make=$1&model=$2&year=$3 [L,QSA,NC]
RewriteCond %{THE_REQUEST} /buy-car\.php\?make=([^\s&]+)&model=([^\s&]+)\s [NC]
RewriteRule ^ buy-car/%1/%2? [R=302,L]
RewriteRule ^buy-car/([\w-]+)/([\w-]+)/?$ buy-car.php?make=$1&model=$2 [L,QSA,NC]
RewriteCond %{THE_REQUEST} /buy-car\.php\?year=([^\s&]+)\s [NC]
RewriteRule ^ buy-car/%1? [R=302,L]
RewriteRule ^buy-car/([\^;0-9-]+)/?$ buy-car.php?year=$1 [L,QSA,NC]
RewriteCond %{THE_REQUEST} /buy-car\.php\?make=([^\s&]+)\s [NC]
RewriteRule ^ buy-car/%1? [R=302,L]
RewriteRule ^buy-car/([\w-]+)/?$ buy-car.php?make=$1 [L,QSA,NC]
what could be the solution.
thanks
Rather than maintaining so many rules like that and possibly more due to optional query parameters I strongly suggest using front controller .htaccess like this:
DirectryIndex index.php
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) index.php?uri=$1 [L,QSA]
Then use $_GET['uri'] in the php code to get requested URI and process them.

URL rewrite with .htaccess for login and profile system

I have a website where the url is localhost/project/profile.php?user=usernameand I am trying to get the url to look like this: localhost/project/username
The most I am able to do is get rid of the .php by using the following code:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
but that is not what I need right now.
Here is the code that is meant to be changing the url - I got it off of another thread.
Options FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?user=$1 [L,QSA]
but it obviously doesn't work.
I need to be able to go to the url: localhost/project/username and it registers as the original URL localhost/project/profile.php?user=username
** THE ANSWER **
Thanks to Howlin
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /project/profile\.php\?user=(.*)\ HTTP
RewriteRule ^ /project/%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user=
RewriteRule ^(.*)$ /project/profile.php?user=$1 [L]
This should work:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /project/profile\.php\?user=(.*)\ HTTP
RewriteRule ^ /project/%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user=
RewriteRule ^project/(.*)$ /project/profile.php?user=$1 [L]
The above will change project/profile.php?user=username to project/username and it will load the information from the project/profile.php?user=username page
EDIT:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /project/profile\.php\?user=(.*)\ HTTP
RewriteRule ^ /project/%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /project/profile.php?user=$1 [L]
Try this:
Options FollowSymLinks
RewriteEngine On
RewriteBase /project/
RewriteRule ^([^/]+)/?$ profile\.php?user=$1 [L,NC,QSA]

How to display specific parameters from link in URL with htaccess?

What.htaccess code should I write to turn something like
localhost/article.php?id=something&number=something
into just
localhost/article/id
Thoughts guys?
You can use rules like this in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+article\.php\?id=([^\s&]+) [NC]
RewriteRule ^ article/%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^article/([^/]+)/?$ article.php?id=$1 [L,QSA,NC]

Categories