How to change URL format using htaccess? - regex

I want to change the format of my URL. Suppose my page is
example.com/add.php?mod=new
I want to change it to
example.com/add/new
I found the following code but it only remove file extension from the url
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

You need another rule to handle query parameter:
RewriteEngine On
# to support /add/new
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?mod=$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Related

Remove the query string from url using htaccess

I need some help with htaccess. Would you be so kind to assist a little bit?
I have a URL like this
https://example.com/index.php?p=application-intelligence
[or]
https://example.com/?p=application-intelligence
Basically the index.php is passed some parameter 'home' to know which page to load i.e. home.php
So I've tried to follow your post on your blog but with not much luck.
So I'd like the final code to be
https://example.com/application-intelligence
Here's my code.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?p=$1
Also for the
https://example.com?p=home
I'd like it to be just
https://example.com
You may use these rules:
RewriteEngine on
# handle /?p=home
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?p=home\s [NC]
RewriteRule ^ /? [R=301,L,NE]
# handle /?p=<something>
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]
This is what i have in my apache virtual host conf file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

htaccess conditional with else RewriteRule

Hello i want to set a RewriteRule checking a url parameter as http://example.com/admin/bla/foo, example:
If my url contains 'admin' then use:
RewriteRule ^(.*)$ index.php [L]
else
RewriteRule ^([a-z]{2})/(.*)$ ?lang=$1 [L,QSA]
I tried:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/?admin$ [OR]
RewriteRule ^(.*)$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/(.*)$ ?lang=$1 [L,QSA]
Any help is appreciated.
You can use these rule in site root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin(?:/|$) index.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/(.*)$ ?lang=$1 [NC,L,QSA]
This assumes there is no .htaccess in admin/ directory.

mod rewrite condition fix to avoid partial word match

Its probably very simple, but I can't make it work =/
I have this rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin/?(.+?)?$ adm.php?route=$1&%{QUERY_STRING} [L]
Well, its suppose to send the user to the adm.php when there is a localhost/admin or localhost/admin/anything url, preserving the query string.
It works as expected, except for the fact it's matching any word that starts with 'admin', for example:
localhost/admin/news/list?page=1 => adm.php?route=news/list&page=1
localhost/adminbool/news/list?page=1 => adm.php?route=bool/news/list&page=1
Both of rewrite works, but that extra 'bool' messes everything in my routing process.
How can I make it respond only to a 'admin' exact match?
Any ideas?
Thanks!
EDIT
I guess im not clear enought.
Here is the full .htaccess and a brief comment about how its suppose to work
Options -Indexes
Options -MultiViews
<FilesMatch "\.(tpl|ini|log|txt)">
Order deny,allow
Deny from all
</FilesMatch>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(adm/|app/|lib/|sys/) - [F,L,NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!admin/?$)(.+?)/?$ index.php?route=$1&%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin(/.*)?$ adm.php?route=$1 [L,QSA]
</IfModule>
the index.php file refers to /app (front-end)
the adm.php refers to the /adm (back-end)
the urls of the front-end has no prefix, while the back-end urls has the /admin/ prefix
example:
localhost/admin - back-end
localhost/admin/ - back-end
localhost/admin/news/list?order=date - back-end
localhost/ - front-end
localhost/admine - front-end
localhost/administration-whatever.html - front-end
localhost/admine123-anything.html - front-end
Couple of issues:
Your regex is incorrect
You're not using helpful QSA flag.
Use this rule with correct regex and QSA:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin(/.*)?$ adm.php?route=$1 [L,QSA]
QSA (Query String Append) flag preserves existing query parameters while adding a new one.
Your rewrite rules:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(adm/|app/|lib/|sys/) - [F,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin(/.*)?$ adm.php?route=$1 [L,QSA]
RewriteCond %{REQUEST_URI} !/adm\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!admin/?$)(.+?)/?$ index.php?route=$1 [L,QSA,NC]
Try this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin[$|/*](.+?)?$ adm.php?route=$1&%{QUERY_STRING} [L]

htaccess to redirect to a different site with a query

Hello I am trying to redirect:
http://example.com/sz/QUERY to:
http://differentsite.com/?q=sea:r.ch QUERY
I am having trouble with adding the space in the above link...
Here is what I got so far in root/sz/:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://differentsite.com/?q=sea:r.ch%20$1 [L,QSA]
Any help would be appreciated.
You can use this rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sz/(.+)$ "http://differentsite.com/?q=sea:r.ch $1" [L,QSA,NE,R]

Rewrite rule with .htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.html$ $1.php
RewriteRule ^(.*)\.html$ page.php?title=$1
I see that my rewriting rules are working but depends on the order of execution. If the pages rewrite rule is the first...it works only for the pages.php.
I've also "played" around with the flags but no results up there.
Try this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)\.html$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)\.html$ page.php?title=$1 [L,QSA]