Htaccess rewrite rule exclude file with extension in the name - apache

I have used the following rewrite rule for a page:
RewriteRule ^([a-z0-9_.]*)$ description.php/$1
I want to match the url like "domain/user_name" which should open the description page.
But, with my current rule, when i am trying to access "abc.php" file, it is also showing the description.php which is different from abc.php file.
Can anyone suggest the solution?

You can tweak regex of your rule:
RewriteCond %{REQUEST_URI} !\.php [NC]
RewriteRule ^([\w.-]+)/?$ description.php/$1 [L]

Related

how to dynamically rewrite filepath .htaccess based on domain?

I have this apache rule in my .htaccess file:
RewriteCond %{HTTP_HOST} ^testURL.localhost.local
RewriteRule ^index.php$ /_testURL/index.php [L]
How would I write this so that testURL2.localhost.local or any other subdomain would be rewritten to the corresponding directories?
For instance testURL2.localhost.local would be rewritten to _testURL2/index.php etc
I already tried the option below but I didn't get the intended result:
RewriteCond %{HTTP_HOST} ^testURL
RewriteRule ^cms.php$ /_%1/cms.php [L]
With your shown samples, attempts; please try following htaccess rules file. I have posted 2 sets of htaccess rules file here, you have to use ONLY one set at a time.
1st solution: This is specifically for host testURL.localhost.local and will look for either index.php OR cms.php only in UI.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?(testURL)\.localhost\.local$ [NC]
RewriteRule ^((?:index|cms)\.php)$ _%1/$1 [NC,L]
2nd solution: A Generic solution, where it will look for anyvalue.localhost.local and it will add anyvalue in path while rewriting, also it will look for any php files in uri.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]*)\.localhost\.local$ [NC]
RewriteRule ^([^.]*\.php)$ _%1/$1 [NC,L]
NOTE1: Also please make sure to clear your browser cache before testing your URLs.
NOTE2: Please keep these rules at the top of your htaccess rules file.
NOTE3: I am also additionally/optimally matching www. in host in case you don't want it you could remove it (?:www\.)? part in condition.

htaccess only one file in subfolder and keeping the url

I need to put files in a new folder like this:
domain.com/newsite/newpanel/events.php
There are other files in the server like:
domain.com/events.php,
domain.com/oldsite/events.php etc.
Requirement 1:
I cannot redirect everything (because old implementations exist) with generic rules,
so I only want to redirect specific urls.
domain.com/events should now skip the old files and go to domain.com/newsite/newpanel/events.php
Requirement 2:
I tried something like this
RewriteRule ^events /newsite/newpanel/events/$1 [P]
but the url on the url bar will change. Is it possible for it to display domain.com/events?
thank you all!
EDIT: Since OP has mentioned different URLs in comments section so adding solution as per that here.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/CProjects/events/?$ [NC]
RewriteRule ^(.*)$ CProjects/folderA/folderB/events [L]
Based on your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs after putting these Rules into your .htaccess file.
RewriteEngine ON
RewriteCond ^/events/?$ [NC]
RewriteRule ^(.*)$ newsite/newpanel/events.php [L]

Rewrite in HTACCESS

Can anyone help me here? I did a rewrite for the english version of my site.
It's was mysite.com/?lang=en And now it's mysite.com/en/
I've tried redirecting the URL with this :
RewriteRule ?lang=en$ /en/ [R=301,L]
But each time I put this line in my HTACCESS, the website goes down.
Your rewrite rule is trying to use the parameter portion of the URL. There is an existing SO answer with an example:
HTAccess Rewrite based upon a Parameter Value, ignore other Parameters
Perhaps something like this will help:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)(lang=en+).+ [NC]
RewriteRule ^en$ %{REQUEST_URI}?%1 [R=302,NE,NC,L]

Proper htaccess rewrite

I'm trying to force an old URL to go to the new url and my code seems to have no effect
RewriteCond %{QUERY_STRING} ^index2\.php?page=shop\.product_details&\.tabs\.tpl&product=310&category=71&Itemid=2$
RewriteRule .* /hunting/back-packs/multi-packs/black-2.5-pack.html [R=301,L]
my old url is
www.mywebsite.com/index2.php?page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2
and my new one is
www.mywebsite.com/hunting/back-packs/multi-packs/black-2.5-pack.html
My code does not break anything but does not work either
Thanks in advance
As I understand you want redirect (301 Permanent Redirect) so the URL will change in browser. This will work for this URL ONLY /index2.php?page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2:
RewriteCond %{QUERY_STRING} =page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2 [NC]
RewriteRule ^index2\.php$ http://www.mywebsite.com/hunting/back-packs/multi-packs/black-2.5-pack.html [R=301,L]
If you want internal redirect (rewrite), then use these lines:
RewriteCond %{QUERY_STRING} =page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2 [NC]
RewriteRule ^index2\.php$ /hunting/back-packs/multi-packs/black-2.5-pack.html [L]
PLEASE NOTE:
You need to put these lines in a proper order (order of rules matters) otherwise (if you put it at the end) some another rule will rewrite it to a different URL.
This needs to be placed in .htaccess file in website root folder. For any other location you may need to modify it a bit.

.htaccess rewrite rule for redirects

I've been fighting this for a while and can't seem to make it work. My old system used a lot of query strings to render pages but they are no longer necessary. My url is below:
OLD URL: www.example.com/links.php?section=5&catid=52
NEW URL: www.example.com/mhfs/links
The name links is coincidental and not necessarily from the old pages name. I need to check which section and catid is present and redirect them to the appropriate page from what it is. I tried the following but this just seems to do nothing. What am I doing wrong?
RewriteCond %{REQUEST_URI} ^links.php$
RewriteCond %{QUERY_STRING} ^section=5$
RewriteCond %{QUERY_STRING} ^catid=52$
RewriteRule ^(.*)$ /mhfs/links? [R=301]
Any help would be GREATLY appreciated.
You must turn the rewrite engine on for it to work.
You probably don't want the regex start symbol on the replacement:
RewriteEngine on
RewriteCond ...
RewriteRule ...