I have a folder/file structure like this:
www.example.com/a/<optional subfolder>/file.png
and I want to have a RewriteRule that passes the "a" as the first parameter and all the stuff after that (file.png or subfolders/file.png) to a file called file.php, so if a user visits
www.example.com/a/file.png (this is not an actual file path, it's somewhere else)
it should be rewritten to
www.example.com/file.php?user=a&file=file.png
and
www.example.com/a/random/subfolders/here/file.png
will be rewritten to
www.example.com/file.php?user=a&file=random/subfolders/here/file.png
I tried a whole lot of RewriteRules I found on the Internet, one of them even worked but not for subfolders. As I do not know one thing about RegEx, I'd really appreciate your help ^^
Try adding this rule to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.+)$ /file.php?user=$1&file=$2 [L,QSA]
Related
I have a shop URL like this
https://domain.eu/cz/lp/9200
And I'm trying to rewrite it in .htaccess to https://domain.eu/lp/index.php?id=$2&lang=$1
I came really close with
RewriteRule ^/?(hr|sk|pl|cz|ro|it)/lp/(\d+)?$ /lp/index.php?id=$2&lang=$1
which works ok but I can't seem to find a way to handle the situation when there is no lang in URL.
So this is also valid: https://domain.eu/lp/9200 but in that case I want $1 to just be empty (or have a default value when it's not present)
I know "?" means "one or zero" times that's why I tried
RewriteRule ^/?[(hr|sk|pl|cz|ro|it)?]/lp/(\d+)?$ /lp/index.php?id=$2&lang=$1
But it doesn't work as expected. Any point in the right direction would be appreciated.
With your shown samples, attempts; please have your htaccess Rules file in following manner. Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rewrite rule for uris which have only 1 parameter.
RewriteRule ^lp/(\d+)/?$ /lp/index.php?id=$1 [NC,L]
##Rewrite rule for uris which 2 parameters.
RewriteRule ^(hr|sk|pl|cz|ro|it)/lp/(\d+)/?$ /lp/index.php?id=$2&lang=$1 [NC,L]
OR use following solutions, in case uris you are trying to access are non-existent ones. Make sure either use 1st solution OR this one, once at a time only.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^lp/(\d+)/?$ /lp/index.php?id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(hr|sk|pl|cz|ro|it)/lp/(\d+)/?$ /lp/index.php?id=$2&lang=$1 [NC,L]
I've a problem with one htaccess rule, i need that if someone call an non existent url with a specific syntax like:
http://www.somesite.com/somefolder/somefolder/123456978/unexistant_folder_nor_file_2015
where http://www.somesite.com/somefolder/somefolder/ exist and
123456978/unexistant_folder_nor_file_2015 does not exist
the URL called will be passed to a script with below syntax:
http://www.somesite.com/somefolder/somefolder/index.php?id=/12345678/unexistant_folder_nor_file_2015
(where somefolder is always a folder with same name like, as example, test)
i tried below code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^somefolder/([^/]+)/([^/]+)$ /somefolder/somefolder/index.php?id=$1 [L, NC]
but doesn't work can someone give me some advice to solve this problem?
thank you
You can use this rule inside /somefolder/somefolder/.htaccess:
RewriteEngine On
RewriteBase /somefolder/somefolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\d+/.*)$ index.php?id=$1 [L,QSA]
I try to make nice URLs with following htaccess entries:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w]+)/?([\w]+)?$ /index.php?subdir=$1&page=$2 (works fine)
#RewriteRule ^([^/]+)/?([^/]+)?$ /index.php?subdir=$1&page=$2 (doesn't work: 500 Int.Serv.Error)
(This one isn't much important to me, since, the first regex works, but if I understand this problem maybe the next problem I understand too)
So with the following line I'll try to get the rest of the query string
#RewriteRule ^(\w+)/?([\w]+)?(.*)$ /index.php?subdir=$1&page=$2&$3 (doens't work: 500 Int.Serv.Error)
Can someone explain, what I'm doing wrong? I have read something about that a wrong regex can create an endless loop. Maybe I've got one or two here?
Thanks in advance and regards
doc
The query string isn't available as part of the RewriteRule, you'd have to capture and examine it in separately in a RewriteCond %{QUERY_STRING} ...
However, you don't need to actually need to capture the rest of the query string in the rewrite, you can just use the QSA or Query String Append flag in your rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w]+)/?([\w]+)?$ /index.php?subdir=$1&page=$2 [QSA]
I plan on learning the ins and outs of mod_rewrite, but I have a problem I would like solved before I get around to doing that, and I'm probably only missing something small here anyway.
I want to force redirect any directory that contains digits [0-9]{1,4} to a single php file with a querystring of that number.
For example
http://example.com/23
or
http://example.com/23/
would redirect to:
http://example.com?23
(the .php file is my index)
Currently I have the following:
RewriteCond $/([0-9]{1,4})/^
RewriteRule $/([0-9]+)^ ?%1 [R]
Which throws a 500...
Thanks for your time.
Try with
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]{1,4})$ ?$1 [QSA,L]
I'm new to playing with .htaccess for nicely formatted urls and I'm just not sure I'm doing it right.
My current .htaccess file looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/(.*) search.php?query=$1 [L]
RewriteRule !\.(gif|jpg|ico|css|js|txt|xml|png|swf)$ index.php
What I want is for mysite.com/home/56/page-title to go to index.php where I filter out the number and load the correct index page, which works fine.
Before that, I want to check if the url is pointing at search.php, and if so redirect to mysite.com/search/the-search-term, this also works fine.
What isn't working is if I try to visit a specific .php file say mysite.com/control_panel.php - it just takes me to index.php, but I thought that the following line stopped that from happening?
RewriteCond %{REQUEST_FILENAME} !-f
If someone could explain it to me that would be great :)
Thanks!
1. Order of rules matters
2. RewriteCond directives will only be applied to the ONE RewriteRule that follows it. If you need to apply the same conditions to multiple rules you have to write them multiple times or change the rewrite processing logic (multiple approaches available).
Try this one:
RewriteEngine On
RewriteRule ^search/(.*) search.php?query=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(gif|jpg|ico|css|js|txt|xml|png|swf)$ index.php [L]