I'm trying to create some 'friendly URL' to my site. But I'm not sure what to do. Below as my site is structured:
To enter a page for example: localhost/study/dashboard/client/network.php
I wanted to write to localhost/study/network. Following the same format to some other pages. I wanted to block direct access to critical files, such as user-proc.php, api.php for example.
What I tried to do:
RewriteEngine On
RewriteRule ^study/network?$ network.php
I put a .htaccess file in each directory (administration, client), but that blocked my website for full. I could not even more access for localhost/study/administration/network.php.
To rewrite from /study/network to /study/dashboard/client/network.php, you can just put the two URLs, pattern and substitution, in a RewriteRule
RewriteRule ^study/network$ /study/dashboard/client/network.php [L]
This is for this specific case. To do this for any combination, you must capture the two parts from the request URI pattern and then use this in the substitution part
RewriteRule ^(.*?)/(.*?)$ /$1/dashboard/client/$2.php [L]
This looks for any two component part (...)/(...) and uses this - $1 and $2 - in the replacement.
To prevent access to some URI, you do almost the same, but omit the substitution part with a - (dash), and use the flag F|forbidden
RewriteRule user-proc.php - [F]
Related
I already use this htaccess rule already and it works
RewriteRule ^community/profile/([^/]+) community/profile/profile.php?id=$1
So, when I'm on a profile page for example website.com/community/profile/example-user, I will be able to extract the user from the id=$1 part.
I need to extend this to two folders, where example-user can have 2 sub-folders, like website.com/community/profile/example-user/folder1/ and website.com/community/profile/example-user/folder 2/
I tried the following rule:
RewriteRule ^community/profile/([^/]+)/([^/]+) community/profile/profile.php?id=$1&car=$2
Unfortunately though, this breaks the whole rule and it doesn't work. I got the idea from this post https://webmasters.stackexchange.com/questions/65785/rewrite-up-to-three-folders-into-three-query-string-parameters-to-a-php-script
Also, I tried
RewriteRule ^community/profile/../([^/]+) community/profile/profile?id=$1&car=$2
Also didn't work.
To make things clear, if one only goes to the 'first' folder, in this example it is /example-user/, the car variable should just be empty. How can I write this rule so that it will put that second 'folder' into a second variable so I can extract it later in my php file?
Make sure to use anchor $ for precise matching in regex
Include dot in your character class to avoid matching it again
Rules :
RewriteRule ^community/profile/([^/.]+)/?$ community/profile/profile.php?id=$1 [L,QSA,NC]
RewriteRule ^community/profile/([^/.]+)/([^/.]+)/?$ community/profile/profile.php?id=$1&car=$2 [L,QSA,NC]
I am trying to figure out how to setup some redirects in my htaccess file.
We load our stores with dynamic urls for example:
/locations/new-york
new-york being the dynamic part
We have old urls that contain different path such as
/locations/new-york/local-store
I want to be able to take all locations/dynamic-city and forward them to the new URL
So
/locations/new-york/local-store/offers
should goto /locations/new-york/current-offers
RewriteRule ^locations/(.*)/local-store/offers locations/$1/current-offers [L,R=301,QSA]
RewriteRule ^locations/(.*)/local-store/hot-tubs locations/$1/hot-tubs [L,R=301,QSA]
RewriteRule ^locations/(.*)/local-store/pre-owned locations/$1/pre-owned [L,R=301,QSA]
RewriteRule ^locations/(.*)/local-store/client-reviews locations/$1/reviews [L,R=301,QSA]
Ok, so I'm not seeing a way to easily use 1 rewriterule to cover all 4 cases. But you can see make sure the requested url following a pattern like this.
^locations/{location}/local-store/offers
Then you take the location and place it into the new url you want to write it to.
L # means it's the last rule, don't try to match anymore
R=301 # means redirect the old url to the new one
QSA # means append any query string parameters to the new url
I have a very small question. I created a .htaccess file that suppose to rewrite a condition example.com/user/foo/bar as example.com/user.php?username=$1&tab=$2 so username name will be foo and the viewing tab must be bar.
But when there is no bar data provided exactly like example.com/user/foo/ it goes to user with default tab (default tab is generated in script.php file).
But I saw some website that they can do it just like example.com/user/foo, without the / at of the line. When I do example.com/foo to go to user, it says page not found. My correspoding .htaccess line is:
RewriteEngine On
RewriteRule ^user/(.*)/(.*)$ user.php?username=$1&tab=$2 [L]
Please help me with this sitiation.
Your match rule is ^user/(.*)/(.*)$, which means that the second slash must be there (since it's specified), but the text afterwards is optional (since * matches zero or more characters).
Really, I'd add two rewrite rules to do both differently (and to prevent using complicated regex), e.g.
RewriteRule ^user/(.*)/(.*)$ user.php?username=$1&tab=$2 [L]
RewriteRule ^user/(.*) user.php?username=$1 [L]
Alternatively, you can make the 2nd parameter optional:
RewriteRule ^user/([^/]+)/?(?:(.*)|)$ user.php?username=$1&tab=$2 [L]
As long as you don't mind having no value for $_GET['tab']
For my site let's say I have a file in a folder called 'download'
so the direct link for the file would be www.testing.com/download/file.exe
I want to throw an authorization token with it like so, .../download/file.exe?auth=asdffg122**, with the auth token being a hash from time and expiry. So far I have written in a .htaccess file :
RewriteEngine on
RewriteRule \(.*)$.(exe)$ check.php?file=$1?auth=$2 [QSA]
I want to pass the name of the file to variable $1, and the auth token to the $2 variable. Is there a way to do this?
like this?
RewriteEngine on
RewriteCond %{QUERY_STRING} .*auth=(.*)\&*.*$
RewriteRule ^.*\/(.*\.exe)$ check.php?file=$1&auth=%n
Assuming you add the auth= to the GET request
Edit: Modified based on comment from $#Orbling
What you probably wanted to, or should write is:
RewriteRule ^(.*\.exe)$ check.php?file=$1 [QSA]
The leading \ backslash should be a ^ start anchor, if you place that Rewriterule into the download folder. You can also only have one $ end of subject marker (usually). To assert the file extension, move it into the parenthesis. The whole of (.*\.exe) becomes $1.
The ?auth=abc22 is automatically appended as &auth=abc22 when you use [QSA]. So that adding a $2 becomes redundant. (You would need a separate RewriteCond to match the QUERY_STRING anyway. It's not part of the rewrite path.)
See also Serverfault: Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask? for more examples.
Situation:
I have a few hundred posts each belonging to a particular category.
A] Now when the user visits the home page, the content is irrespective of the category sorted by date.
http://www.example.com
He can navigate through different pages like:
Type 1: http://www.example.com/3 which corresponds to http://www.example.com/index.php?page=3
I can probably do this in mod_rewrite
B] The user can then decide to view by category like:
Type 2: http://www.example.com/Football which will correspond to
http://www.example.com/index.php?page=1&category=Football
He can then navigate through pages like:
Type 3: http://www.example.com/Football/5 which =>
http://www.example.com/index.php?page=5&category=Football
C] I have a directory called View with index.php in it. It only shows individual posts like:
Type 4: http://www.example.com/View/1312 => http://www.example.com/View/index.php?id=1312
Here is the mod_rewrite I do:
RewriteEngine on
RewriteRule ^View/([^/.]+)/?$ View/index.php?id=$1 [L]
Now here are the problems I have
In point C]: http://www.example.com/View/1312 works fine but http://www.example.com/1312/
(notice the trailing slash) breaks apart & gives weird results.
Q1) So how do I maintain consistency here?
Q2) Ideally I would want http://www.example.com/View/1514 to show a 404 Error if there is no post with id 1514, but now I have to manually take care of that in PHP code.
What is the right way of dealing with such dynamic urls? especially if the url is wrong.
Q3) how do I ensure that http://www.example.com & http://www.example.com/ both redirect to http://www.example.com/index.php?page=1; (mod_rewrite code would be helpful)
Please Note that there are only two index.php files. One in the root directory which does everything apart from showing individual posts which is taken care by a index.php in View directory. Is this a logical way of developing a website?
Try these rules:
RewriteRule ^$ index.php?page=1 [L]
RewriteRule ^([0-9]+)/?$ index.php?id=$1 [L]
RewriteRule ^View/([0-9]+)/?$ View/index.php?id=$1 [L]
RewriteRule ^([A-Za-z]+)/?$ index.php?category=$1&page=1 [L]
RewriteRule ^([A-Za-z]+)/([0-9]+)/?$ index.php?category=$1&page=$2 [L]
As for the other question: Yes, since Apache can map these requests to existing files, it responds with a success status code. Now if your application decides that the requested resource does not exist, you need to handle that within your application and send an appropriate status code.
To fix the trailing slash, Just put /? before the $ at the end in your pattern