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']
Related
On the home page of my website, I have a guide that includes some links, and each redirects to a family guide page, all with the redirection by identification of each element of the guide, remaining as the link
https://example.com/family-guide/?id=4
Where '4' is the ID of the element.
I would like to leave like this:
https://example.com/family-guide/4
I tried to use mod_rewrite in the .htaccess file:
RewriteRule ^family-guide/([0-9] +)/([0-9a-zA-Z_-]+) family-guide/?Id=$1name=$2 [NC, L]
but nothing happens
Your rule has some spaces. Be careful because if there is a space between the flags [NC, L] the rule is not valid.
Another thing is that you want the url https://example.com/family-guide/4 to match a rule and your rule expects two parameters (id and name) while in this url there is only one.
I would use a couple of rules instead:
RewriteRule family-guide/([0-9]+)/([0-9a-zA-Z_-]+) family-guide/?id=$1&name=$2 [NC,L]
RewriteRule family-guide/([0-9]+) family-guide/?id=$1 [NC,L]
With this rules if you go to https://example.com/family-guide/4 you should be shown the content of https://example.com/family-guide/?id=4. And if you go to https://example.com/family-guide/4/whatever it will show you the content of https://example.com/family-guide/?id=4&name=whatever instead, which is what I understood you need.
Also, the rule order is important as if they were in the opposite order https://example.com/family-guide/4/whatever would match the other rule and show the content of https://example.com/family-guide/?id=4/whatever
Lately I have changed the way my website works - physical page for every article vs. dynamically loaded content without physical (sub)page, but I realized I cannot simply upload the new site files cos I would break up all the social platform sharing links, counters and stuff as there are literally thousands of the subpages.
I heard (I know about it) that via .htaccess and RewriteEngine (I need using RewriteEngine as all the code in htaccess is made for it) I can make pages load internally something completely different depending on the actual URL, like, for example, if I have actual URL link to one of my subpages:
http://sub.mypage.com/php/somearticle.php?j=en
...so without changing the text of the URL it would load my new site files internally on different principle, like this:
http://sub.mypage.com/?s=somearticle&j=en
Now I also need that those variables "j" and the "somearticle" to be dynamic, or better said they need to be copied exactly as they are from the physical URL in the addressbar (where "somearticle" is actually name of the originally physical php file and the "j" is just language variable) as it will be something else every time so I do not have to make thousands of lines in htaccess for every single concrete subpage - I need some universal code that would manage all the subpages (as the principle is the same for all, just php names changes and sometimes language = variable "j"), you see?
So can anyone help me telling me the exact syntax/code for this to achieve?
EDIT
So I was playing with it myself a bit and this seems to work if I set the subpage manually (NOTE: just a clarification - this is for my localhost:8081 therefore I have in place that 1st condition cos for server I have different version that has different path to index.php) + I slightly updated variable j part thanx to #Ben's post:
# LOAD PAGE DIFFERENT INTERNALLY - LOCALHOST
RewriteCond %{HTTP_HOST} ^localhost:8081 [NC]
RewriteCond %{REQUEST_URI} /php/(.*)\.php$ [NC]
RewriteRule ^php/(.*).php$ /WWW/_PHP_/lego/index.php?s=$1 [QSA,L]
But unfortunately for some reason it affects every page on my site not only pages under /php/, so when I click to go to my first (default/initial = index.php) page it breaks it (it holds summary of all articles - they are not loaded) - anyone knows why, please?
SOLVED
So after small change to #Ben's code this is the right solution thus I take his solution as the right one (as it would actually work OK right away as it is if the page would be on server cos my test version is on my localhost where the path to /php/ directory is different))
LOAD PAGE DIFFERENT INTERNALLY - localhost
RewriteCond %{HTTP_HOST} ^localhost:8081 [NC]
RewriteCond %{REQUEST_URI} /php/([a-zA-Z0-9\-]+).php [NC]
RewriteRule ^ index.php?s=%1 [QSA,L]
REQUEST_URI is path component of the requested URI such as /php/somearticle.php, but not contains query string such as ?j=en.
The RewriteCond pattern, ! character (exclamation mark) is used to negate the result of the condition. To prefix with some pattern, use ^ character (caret), matches the beginning of a line.
%1 is the RewriteCond backreference that provides access to the grouped parts (in parentheses) of the pattern.
QSA flag, if the replacement URI contains a query string, the query string such as ?j=en will be appended to the newly rewrite uri.
RewriteCond %{REQUEST_URI} ^/php/([a-zA-Z0-9\-]+).php [NC]
RewriteRule ^ index.php?s=%1 [QSA,L]
See also: Apache Module mod_rewrite, RewriteRule Flags
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]
I'm trying to create some nice urls for my php search pages.
my current code:
RewriteRule ^/search-jobs/?$ search-jobs.php [NC,L] # Search jobs page
RewriteRule ^/search-jobs/jobs-in-(.*)/?$ search-results.php?location=$1 [NC,L] # Search results locations page
matches /search-jobs with search-jobs.php, great
but it also matches /search-jobs/jobs-in-london to search-jobs.php, but I want it to match the second rule for search-results.php
Why is the first rule always used? and how to fix it?
EDIT:
none of the current answers have worked. I think my issue is that somewhere on my hosting (not accessible by me) there are some defaults set, as if i just go to /search-results, it will automatically use search-results.php file, although I have set nothing telling it to do so?
So, in theory, any /search-jobs(.*) query will automatically use search-jobs.php, seemingly regardless of any rules I create.
anyway, I rearranged my rules to the below.. and I still always hit search-jobs.php file:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^/search-jobs/jobs-in-(.*)/?$ search-results.php?location=$1 [NC,L] # Search results locations page
RewriteRule ^/search-jobs/(.*)-jobs/?$ search-results.php?keywords=$1 [NC,L] # Search results keywords page
RewriteRule ^/search-jobs/?$ search-jobs.php [NC,L] # Search jobs page
Remove slash / from the beginning of request string like this:
RewriteRule ^search-jobs/jobs-in-(.*)/?$ search-results.php?location=$1 [NC,L] # Search results locations page
RewriteRule ^search-jobs/(.*)-jobs/?$ search-results.php?keywords=$1 [NC,L] # Search results keywords page
RewriteRule ^search-jobs/?$ search-jobs.php [NC,L] # Search jobs page
Put your second rule first, and also enclose your regexes in quotes.
That way when it matches ^/search-jobs/jobs-in(.*)/?$ the [L] flag says don't continue so it won't process ^/search-jobs/?$
If I have a URLhttp://www.domain.com/listing.php?company_id=1 is it possible for me to re-write that to http://www.domain.com/company-name by using that id to pull the name from the database.
Or do I have to change listing.php to make it ?company_name=company-name
If anyone can point me in the right direction that would be great.
Thanks!
A map function allows using no id in the url at all, while still using the id in the rewritten url to do the database lookup.
http://www.domain.com/another-one
maps to
http://www.domain.com/listing.php?company_id=3423
Here is how I use map text files, which I generate from a database query. I believe mod_rewrite also does mapping to a database directly, of which I'm unfamiliar (maybe someone can provide that answer). I use Helicon Tech's isapi_rewrite v3, which works like mod_rewrite, so this should work for you.
Sample map file named map_company.txt
some-company 12
another-one 3423
freds-fill-dirt-and-croissants 44
The rewrite rules:
RewriteMap map_company txt:map_company.txt [NC]
RewriteCond ${map_company:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^/(.*) /listing.php?company_id=${map_company:$1} [NC,QSA,L]
RewriteMap assigns the map_company.txt text file to a variable named map_company (named the same just to be consistent).
RewriteRule is doing the work. It captures everything after the slash into $1, then rewrites it to your listing.php url. The ${map_company:$1} is looking up the url in the map file, and returning the id.
RewriteCond is just doing a double-check to see if the listing is there. The $1 is coming from the RewriteRule url, the NOT_FOUND is a default value if not found, and the condition is if it's not-NOT_FOUND (a little tangled - but it's just checking if it's in the file). If it's in the file, the RewriteRule will be run. If it's not in the file, it skips the RewriteRule, and falls through to more rules (perhaps to a page-not-found or some other default).
You'll need the id in both the urls, but only use the full name in the pretty link for the user.
The following would rewrite http://www.domain.com/42/company_name to http://www.domain.com/listing.php?company_id=42 and just discard the company name.
RewriteEngine on
RewriteRule ^([0-9]*)/([A-Za-z0-9_]*)/$ /listing.php?company_id=$1 [L]
Note that a user could also visit http://www.domain.com/42/wrong_name and still land on the right page with the right company name. If this isn't desired you could change the rule to /listing.php?company_id=$1&company_name=$2 and check for equality in listings.php