I've been trying to rewrite a specific file I have, /u.php/example to just /u/example with htaccess. The /example part I will get and use through php url parsing.
So, another question I read suggested using the following code
RewriteCond %{THE_REQUEST} /(u|a)\.php [NC]
RewriteRule ^ /%1 [L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]
But that does not preserve the /example from /u.php/example when rewritten, and causes a 500 error if I just enter /u/example.
Is there a work around for this? What could be the problem? I haven't been able to find a solution on Stack.
You need to capture and redirect the /example part from URI
RewriteCond %{THE_REQUEST} /(u|a)\.php/([^\s]*) [NC]
RewriteRule ^ /%1/%2 [L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/(.*)?$ /$1.php/$2 [L]
Related
I'm having a conflict with my htaccess file.
I´m using the following code to remove the php handles:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
Everything works well EXCEPT for one subdirectory. When the php handle is removed, it turns out that the ¨handle-less¨ name shares the same name as en existing folder and as a result, the directory for that folder is being displayed, rather than the intended php page. I would rather avoid having to change the folder name especially if this is something that can be addressed from the htaccess file.
(I'm rather a beginner when it comes to coding... so I'm not sure how to best address this issue.)
Any help is appreciated. Thanks :)
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d said : if it is a directory, do not do the rewrite rule
Edit :
Could you try only :
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)/?$ /$1.php [NC]
This should rewrite all the requests with no dots inside to the php file
I am having an URL named www.example.com/print?id=adder
I want it to look like www.example.com/print/adder
I am using the following .htacces code
RewriteEngine on
# redirect all requests except only POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:php?)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^print/([a-z]+) print.php?id=$1 [NC,L]
But still, i am having the URL like www.example.com/print?id=adder.
I am using wamp 3.0.6(Latest Version), PHP 7.0,10 & Apache 2.4.23. Why am i facing this problem. Why isn't the code working?
To redirect your url to cleaner version, put this above your last RewriteRule
RewriteCond %{THE_REQUEST} /print/?(?:\.php)?\?id=([^\s&]+)
RewriteRule ^ /print/%1? [L,R]
If this is a duplicate then I am really sorry, but I used google and also search here and could not find anything that would work for me. Maybe I just don't know what exactly to search for.
Anyway, back to my question:
I am trying to change old ugly URLs to nicer urls (wanted to have news/XXX/XY but relative links defeated me), so I wrote couple lines of code to my .htaccess and tested them (well, I actually used same code on my other page for redirect, so I know it does work). I get redirected from /news.php to /news, but the query string magic is not working at all...
RewriteRule ^news.php$ /news [L,NC,R=301]
RewriteRule ^news?$ /news.php [L,NC]
RewriteCond %{THE_REQUEST} /news\?rowstart=([^\s]+) [NC]
RewriteRule ^ news-row-%1? [R=302,L]
RewriteCond %{THE_REQUEST} /news\?readmore=([^\s]+)&rowstart=([^\s]+) [NC]
RewriteRule ^ news-%1-%2? [R=302,L]
RewriteCond %{THE_REQUEST} /news\?readmore=([^\s]+) [NC]
RewriteRule ^ news-%1? [R=302,L]
None of the RewriteRules after first one gets executed. But if I change my .htaccess like this:
RewriteRule ^news.php$ /news [L,NC,R=301]
RewriteCond %{THE_REQUEST} /news\?rowstart=([^\s]+) [NC]
RewriteRule ^ news-row-%1? [R=302,L]
RewriteCond %{THE_REQUEST} /news\?readmore=([^\s]+)&rowstart=([^\s]+) [NC]
RewriteRule ^ news-%1-%2? [R=302,L]
RewriteCond %{THE_REQUEST} /news\?readmore=([^\s]+) [NC]
RewriteRule ^ news-%1? [R=302,L]
RewriteRule ^news?$ /news.php [L,NC]
then it works. I tried using /\?bla - /news\?bla - /news\.php\?bla etc. Nothing actually worked.
Does somebody have any Idea why its behaving like this and how to fix it? It looks like it does not recognize the news after it starts loading from news.php file.
I am completly lost, as I don't work with .htaccess that often. Any ideas?
I actually ended up using this:
#########################
# NICE URLS QUERIES #
#########################
RewriteCond %{THE_REQUEST} /news(?:\.php|)\?rowstart=([^\s]+) [NC]
RewriteRule ^ news-page-%1? [R=302,L]
RewriteCond %{THE_REQUEST} /news\(?:\.php|)\?readmore=([^\s]+)&rowstart=([^\s]+) [NC]
RewriteRule ^ news-%1-%2? [R=302,L]
RewriteCond %{THE_REQUEST} /news(?:\.php|)\?readmore=([^\s]+) [NC]
RewriteRule ^ news-%1? [R=302,L]
RewriteRule ^news-([0-9]+)-([0-9]+)?$ news.php?readmore=$1&rowstart=$2 [L,QSA,NC]
RewriteRule ^news-([0-9]+)/?$ news.php?readmore=$1 [L,QSA,NC]
RewriteRule ^news-page-([0-9]+)/?$ news.php?rowstart=$1 [L,QSA,NC]
#######################
# OTHER NICE URLS #
#######################
RewriteRule ^news.php$ /news [L,NC,R=301]
RewriteRule ^news?$ /news.php [L,NC]
The first thing you need to make sure you're doing is putting all the redirects first. That is, every rule that has the R flag needs to be before the rules that don't. But it looks like you're not matching the - style URLs to rewrite them BACK to the version with the query string. So I think you really want something like this (you can get rid of the .php extension while you get rid of the query string):
RewriteCond %{THE_REQUEST} \ /+news\.php(\ |$)
RewriteRule ^ /news [L,R=301]
RewriteCond %{THE_REQUEST} \ /+news(?:\.php|)\?rowstart=([\ &]+)
RewriteRule ^ /news-row-%1? [L,R=301]
RewriteCond %{THE_REQUEST} \ /+news(?:\.php|)\?readmore=([^&]+)&rowstart=([\ &]+)
RewriteRule ^ /news-%1-%2? [L,R=301]
RewriteCond %{THE_REQUEST} \ /+news(?:\.php|)\?readmore=([\ &]+)
RewriteRule ^ /news-%1? [L,R=301]
## Now, internally rewrite the request back to the query string and php extension
RewriteRule ^news$ /news.php [L]
RewriteRule ^news-row-(.*)$ /news.php?rowstart=$1 [L,QSA]
RewriteRule ^news-(.*)-(.*)$ /news.php?readmore=$1&rowstart=$2 [L,QSA]
RewriteRule ^news-(.*)$ /news.php?readmore=$1 [L,QSA]
I'm using:
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
This makes it so I don't have to use ".php" at the end of my pages. However, if I don't specify an index it gives a 404.
Example:
example.com/content.php is directed to example.com/content
^ That works perfectly well.
However, if I want to go to a directory like example.com/users it gives a 404 error.
Only example.com/users/index works.
I want to be able to remove .php the way it is doing already, but I still want directories to work without specifying a page(automatically display index.php). I'm not very fluent in regex or htaccess coding so I have been unable to tweek this myself.
Any help would be appreciated. Thank you!
Replace your code with these 2 rules:
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
I want to rewrite url on my site such that,
www.example.com/login points to www.example.com/login.php
www.example.com/login/ too points to www.example.com/login.php
Pages deep inside the site should follow the above rule. i.e
www.example.com/inner/inner2/something
and www.example.com/inner/inner2/something/ should also point to
www.example.com/inner/inner2/inner3/something.php
www.example.com/login/anything , if doesn't exist should show 404 (as in my case it's causing loops)
The rule I have written is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*[^/])$ $1.php [L]
RewriteRule ^(.+)/$ $1.php [L]
It works fine, in 3 of the above cases but in 4th it's causing loops.
Also I want to show an 404, if a file with .php extension is requested.
And also, the behaviour of above rule changes if I use REQUEST_URI instead of REQUEST_FILENAME. What's the difference between the two?
TIA
Try this rule for php extension hiding:
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]