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
Related
I am looking for a solution to do bulk 301 redirects in WordPress. I have looked at some common htaccess redirects but cannot find the right solution for what I need.
Current URL format:
website.com/year/month/post-name (e.g website.com/2020/02/post-name)
Format I would like to achieve:
website.com/blog/post-name
The post slug will always remain the same, it's only the first part of the URL that needs adjusting.
You could match the format of the request URI like year/month/post-name with regex in RewriteCondition and have a rewrite rule for it if the request URI matches.
Snippet:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/?\d+\/\d+\/.+ [NC]
RewriteRule ^\d+\/\d+\/(.+) http://website.com/blog/$1 [NC,L,QSA]
Demo: https://htaccess.madewithlove.be?share=f2c421fe-e23d-50a9-8988-9a7bac647951
In the above rule, we perform a case insensitive match and grab the post-name part in group 1 of regex which is (.+). Now, we add that in our redirection URL with a $1 where the 1 is the group number(as group number 0 is the entire regex itself).
Update:
You can add a R redirection signal with status code of 302 if this is a temporary redirect, or a permanent redirect 301 (which will updated in SEO crawl engines as well).
To do so, you can change the rewrite rule from
RewriteRule ^\d+\/\d+\/(.+) http://website.com/blog/$1 [NC,L,QSA]
to
RewriteRule ^\d+\/\d+\/(.+) http://website.com/blog/$1 [R=302,NC,L,QSA]
I want to redirect the following sets of links:
a/b/c or a/b/c/ to a.php?b=c
x/y1/z1/y2/z2 or x/y1/z1/y2/z2/ to x.php?y1=z1&y2=z2
using htaccess and mod rewrite in a standardized general format associating the appropriate PHP get tags and values to the corresponding SEO-friendly link. How do I do so?
I've tried tinkering around with RewriteCond and REQUEST_FILENAME but just cannot seem to get it to work.
Something like this might help:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/? /$1.php?$2=$3 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/? /$1.php?$2=$3&$4=$5
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/?$
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']
I have a website where my links look like this
http://www.domain.com/index.php?lang=English&inc=canyoning
I managed to write rewriteRule like this:
RewriteRule (German|English)\/(.*) http://www.domain.com/index.php?lang=$1&inc=$2 [NC,R]
Now my links look like this: <a href="http://www.domain.com/English/canyoning">...
This works, but I can see non user friendly URL in browser address bar. How can I tell browser to use link like /English/canyoning in URL and not index.php?lang=English&inc=canyoning?
And second: I would like to use forms on page.
There is no difference, whether I use form method =GET or POST, No variables come to destination site.
I guess there is my rewriteRule wrong. How to fix those issues?
Thank you for help!
This is because you are doing a redirect not a rewrite.. (The R flag indicates a Redirect)
So remove the R flag should fix your issue.
You may also need to remove the hardcoded domain. As you are doing a rewrite you cant rewrite to a different domain.
ie. Change
RewriteRule (German|English)\/(.*) http://www.domain.com/index.php?lang=$1&inc=$2 [NC,R]
To
RewriteRule (German|English)\/(.*) /index.php?lang=$1&inc=$2 [NC]