htaccess URL rewrite file/param/value format - apache

I am writing a htaaccess rule to rewrite my below URL
http://example.com/edit_user.php?enc=aWQ9MQ%3D%3D
as
http://example.com/edit_user/enc/aWQ9MQ%3D%3D
I tried with http://www.generateit.net/mod-rewrite/index.php but it gives me something like this
RewriteEngine On
RewriteRule ^enc/([^/]*)$ /edit_user.php?enc=$1 [L]
But this is fixed rule for edit_user.php file, I want a rule that would work for all files in same fashion.
Also if user types in http://example.com/edit_user/ the page should redirect to http://example.com/edit_user.php since there are no parameters.

Have this code in root .htaccess:
RewriteEngine On
RewriteRule ^([\w-]+)/enc/([^/]+)/?$ $1.php?enc=$2 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w-]+)/?$ $1.php [L]

Related

Rewrite URL using .htaccess by replacing /? with?

How can I rewrite URL (i.e. remove last / after test) using .htaccess on php page from
from www.example.com/test/?sku=23456&qty=3 to www.example.com/test?sku=23456&qty=3
from www.example.com/page2/?page=3&emp=543 to www.example.com/page2?page=3&emp=543
from www.example.com/stream/?start=4&id=tdfcs45s&q=sat to www.example.com/stream?start=4&id=tdfcs45s&q=sat
I tried but it doesn't work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
With your shown samples and attempts please try following .htaccess rules file. We need to use THE_REQUEST variable here of apache. Check this if this helps you, also clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s(/test)/(\?sku=(\d+)&qty=\d+)\s [NC]
RewriteRule ^ %1%2 [L]

.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

htacess rewrite rules conflict

I am having issues trying to set up a correct htaccess file.
What I basically want to do is to implement clean URL and hide the .php extension except for one file.
What I currently have set up is the following:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
#RewriteRule ^(?!recruit)(.*)$ $1.php [NC,L]
The first rule will take anything after 'recruit' and pass it as a get variable in the url
www.example.com/recruitHI --> www.example.com/recruit.php?id=HI
What the other rule needs to do is to append .php to anything else other than anything that starts with recruit.
www.example.com/index --> will look for index.php
www.example.com/contact --> will look for contact.php
www.example.com/recruit --> Needs to be ignored because of the first rule
When I have the 2 rules on and start apache, I get an error saying my configuration is wrong. They both work individually though.
You can use:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
2nd rule will add .php extension only for URIs that are not file or directories and that are already valid php files.
Try adding a condition to the second rule so that it won't blindly append a php to the end of the URI:
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Specifying RewriteBase at the beginning. If everything is in the site root it would be
RewriteBase /
Otherwise all your Rules should begin with ^/
RewriteEngine On
RewriteBase /
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
RewriteRule ^(.*)$ $1.php [NC,L]
Since your have the L The rules after recruit will only affect items that don't have it. But instead of having a script for every url possibility, you should look at using a single Front Controller.
RewriteEngine On
RewriteBase /
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
RewriteRule ^(.*)$ index.php [NC,L]
Then you can use FallBack provider instead (newer in Apache)
RewriteEngine On
RewriteBase /
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
FallbackResource /index.php

apache mod_rewrite affecting files inside sub folder

Options +FollowSymlinks
RewriteEngine On
RewriteBase /mysite
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ page.php?p=$1&name=$2 [NC,L]
RewriteRule ^([^/\.]+)/?$ page.php?name=$1 [NC,L]
The above code is written in my .htaccess file. I want my URL
from:
http://localhost/mysite/page.php?p=categoryname&name=article_title
http://localhost/mysite/page.php?&name=articel_title
to:
http://localhost/mysite/category/article_title
http://localhost/mysite/article_title
Those rules are working fine but if I try to access my back-end for the admin which URL is http://localhost/mysite/admin` it redirects the page to page.php?name=admin.
Is there a way to just limit the rule only for the page.php and not affecting the other pages under sub-folder which is admin?
You could use a rule to stop /admin from beein rewritten:
RewriteRule ^admin$ - [L]
Put that rule before the others and it will be the only rule that is applied to /admin.
If you add this rewrite condition before your rules, it will only apply the rules if the request is not for a real file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ page.php?p=$1&name=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/\.]+)/?$ page.php?name=$1 [NC,L]

mod_rewrite to a directory

Im trying to create a rewrite rule to a different directory, but unfortunatly its not working.
My current rewrite sends everything back to the index.php file unless the first word in the query string is admin. The rewrite rule for 'admin' is to admin.php, but i actually want it to go to /var/www/html/website.com/admin/admin.php (instead of the file in the same directory).
This is current .htaccess file.
RewriteEngine On
RewriteRule ^(.*\/?).*(css|images|js/)+.*$ - [L]
RewriteRule ^\/?admin\/?(.*)$ admin.php?url=$1 [QSA,L]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
If i change the rewrite rule for admin to use a different directory..
RewriteRule ^\/?admin\/?(.*)$ /var/www/html/website.com/admin/admin.php?url=$1 [QSA,L]
It breaks the site. Any ideas?
Thanks.
It's actually quite simple, you're using the system path, while web path is needed.
Try the following line:
RewriteRule ^\/?admin\/?(.*)$ /subfolder/admin.php?url=$1 [QSA,L]
Try these rules:
RewriteRule ^admin($|/(.*)) admin/admin.php?url=$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?url=$0 [QSA,L]