I'm trying to get the requested filename without the path with htaccess for a RewriteCond.
REQUEST_FILENAME returns the full absolute path, but I only need the filename like test.php
I've been searching for this a lot but couldn't find anything that helped me out.
Thanks for any responses in advance!
Edit:
Im trying to do something like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond _%{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]*)$ _$1.php [L]
The URL typed in the browser looks like this: http://example.org/test
The File that will be requestested by the RewriteRule is: http://example.org/_test.php
With RewriteCond _%{REQUEST_FILENAME}.php -f i tried to check if the file exists first
Basically I want to do this:
URI: /test/blah
Check if _test.php exists (with underscore!)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/_$1.php -f
RewriteRule ^([^/]+)$ _$1.php [L]
I changed * to + so requests for e.g. example.com/ will not redirect to _.php
Related
I am trying to achieve that a url like
example.com/path1 to example.com/index.php?p1=path1
example.com/path1/path2 to example.com/index.php?p1=path1&p2=path2
In my .htaccess I have:
RewriteEngine On
RewriteRule ^(.*)/(.*)$ /index.php?c=$1&g=$2 [NC,L]
RewriteRule ^([a-zA-Z0-9-_]+)$ /index.php?g=$1 [NC,L]
It works well for the case where we have the example.com/path1/path2 but for the the case example.com/path1 only works if there is no dot in the url. For example, I want to example.com/mydomain.com working to example.com/index.php?g=domain.com but I am not able to make it working.
Could you help me with this please?
here's how I would handle this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1
then with whatever serverside language you're using I'll use PHP parse $_GET['param'].
$params = explode('/', $_GET['params']);
This is my simple htaccess code:
RewriteRule ^books/([A-Za-z0-9.]+)$ library/search.php?zig=$1 [NC,L]
My code is working fine for the followings:
mydomain/books/math
mydomain/books/english
mydomain/books/physics.applied
as
library/search.php?zig=math
library/search.php?zig=english
library/search.php?zig=physics.applied
But my code is not working only for
mydomain/books/
it is acting as
library/search.php?zig=index.php
There is no subject named index.php. I want to remove this index.php. My search function should not work for mydomain/books/
You can add a RewriteCond to ignore all files and directories from rewrite:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^books/([A-Za-z0-9.]+)$ library/search.php?zig=$1 [NC,L,QSA]
I'm trying to use the Url rewrite using "RewriteRule".
I would use this link to show a new article in my blog:
https://www.example.com/article/how-to-visit-italy-in-winter
The original script (including vars) is
https://www.example.com/post.php?idpost=how-to-visit-italy-in-winter
This is my .htaccess
RewriteEngine on
RewriteRule ^article/$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .$ https://www.example.com/post.php?idpost=$1 [L]
but it doesn't work, the browser redirect to:
https://www.example.com/post.php?idpost=
I'm trying to resolve about 2 days witout success :(
Thank you.
You rule looks fine except for two things . Your $1 back reference is undefined and empty as you are not capturing any URI. To capture parts of the uri you need to use Regex capture-groups (.*) .
2) You need to use an absolute path instead of the full url as your RewriteRule`s destination if you want to silently redirect the url.
RewriteEngine on
RewriteRule ^article/$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule article/(.+) /post.php?idpost=$1 [L]
I have this problem, I'm using my url like this:
If somebody is coming to the website from a referral the have something like https://myweb.site/UERHF723R so my htaccess have this:
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?ref=$1 [L,QSA]
but how I can send somebody to https://myweb.site/foldername without send them to index.php as a variable.
I think I can use something like this:
RewriteCond %{REQUEST_URI} foldername
RewriteRule .* /foldername/index.php
But I don't want to to this for every new folder, any suggestions?
You can use:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?ref=$1 [L,QSA]
In this way, you only rewrite urls that are not directories (-d) or files (-f)
The second line is therefore not mandatory in your case.
My .htaccess file is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /art/art.php?galerie=$1
So thanks to this I can access the different galeries that exist :
website.com/art/sun or website.com/art/beach for example ( as long as the galerie exists otherwise returns a error message saying page doesn't exists)
This all works fine up to here .
But i needed to add a $_Get on the page also :
website.com/sun?row=2 for example.
But i cannot do this as sun?row=2 isn't a registered galerie ...
How do I get around this problem ?
You can use the QSA (Query String Append) flag to forword new query strings to the target :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /art/art.php?galerie=$1 [QSA]
Now This will rewrite
/foo?querystring
to
/art/art.php?galerie=foo&querystring