htaccess redirect dynamic urls - .htaccess

I am trying to write a htaccess redirect, but it is not working as I want it to.
My current (working) htaccess redirects all html pages with 1 character to the index file as:
RewriteRule ^([a-zA-Z0-9]).html$ index.php?letter=$1
So a.html gets redirected to index.php?letter=a
Now I need to redirect the page a.html?page=2 to index.php?letter=a&page=2
Basically I want to redirect the url, but leave the dynamic part intact.
All of the following return a 404:
RewriteRule ^([a-zA-Z0-9]).html?page=([0-9]+) index.php?letter=$1&page=$2
RewriteRule ^([a-zA-Z0-9]).html?page=(.*) index.php?letter=$1&page=$2
RewriteCond %{QUERY_STRING} page=(.*)
RewriteRule ^([a-zA-Z0-9]).html(.*) index.php?letter=$1&page=%1
I think I'm close, but I can't seem to get there :/
could anyone give me the last push?

Your RewriteRule needs to be
RewriteRule ^([a-zA-Z0-9])\.html$ index.php?letter=$1 [QSA,NC,L]
Please, note that URL parameters are not available for matching within the RewriteRule. If you simply need to append an extra URL parameter you can do so along with the [QSA] flag which would take care of appending the original URL parameters for you.
Please, note that the dot before html needs to be escaped \. as well. The [L] makes sure that rewriting stops and no further rules (if any below) are applied.

Related

Rewriting old and new urls to same page with .htaccess

Have a page currently with the URL /results-details.php?mls_number=stringofnumbers.
I want it to re-write to: /results-details/stringofnumbers
However I want that to basically resolve back to the original page.
I'm also changing all the URLs on the site internally to point to the URL. So I have two re-write rules:
RewriteCond %{QUERY_STRING} mls_number=([0-9]+)$
RewriteRule (.*) /new-homes/results-details/%1? [R=301,C]
RewriteRule ^results-details/([0-9]+)$ results-details.php?mls_number=$1
The second rule works fine on it's own with internal links in the /results-details/stringofnumbers form, but the first one doesn't chain properly to the second one and not sure what I'm doing wrong.
Basically trying to retain any links to the old URLs that might be out there but start using the new URS internally.
Suggestions?
YOu need to match against the actual request and not the URI, because the rewrite engine loops:
RewriteCond %{THE_REQUEST} \ /new-homes/results-details\.php?mls_number=([0-9]+)
RewriteRule ^ /new-homes/results-details/%1? [L,R=301]
RewriteRule ^results-details/([0-9]+)$ results-details.php?mls_number=$1

Targeting single directory for rewrite rule

I have edited this question to use the actual URLs. I need the url
http://westernmininghistory.com/mine_db/main.php?page=mine_detail&dep_id=10257227
To be rewritten like
http://westernmininghistory.com/mine_detail/10257227/
I have tried
RewriteRule ^([^/]*)/([^/]*)/$ /mine_db/main.php?page=$1&dep_id=$2 [L]
Which works on this page but breaks every other page on the site. I was wondering if there was a way to force the rewriterule to only operate on files within the mine_db directory. I had tried RewriteCond but with no success:
#RewriteCond %{REQUEST_URI} ^/mine_db
I really don't know they proper syntax for this though. Any ideas?
First of your rule can be shortened and written without needing RewriteCond. Also it appears that you want to capture 2 variables after test_db.
You can try this rule instead:
RewriteRule ^(mine_detail)/([0-9]+)/?$ /mine_db/main.php?page=$1&dep_id=$2 [QSA,L,NC]
Which will work with URIs like /mine_detail/12345 (trailing slash is optional). Also note that above rewrite will happen silently (internally) without changing the URLi in browser. If you want to change URL in browser then use R flag as well like this:
RewriteRule ^(mine_detail)/([0-9]+)/?$ /mine_db/main.php?page=$1&dep_id=$2 [QSA,L,NC,R]

Redirect when two strings are matched

Trying to make the url:
www.google.com/forum.php?fid=5
Redirect to:
www.google.com/new.php?fid=5
But also need it to keep everything else intact because for example the link can be:
www.google.com/forum.php?fid=5&sortby=asc
And need sortby portion to be there upon redirect.
What the redirect needs to do is look for forumdisplay.php and fid=6 and when both are found in the same url it redirects to blog.php and removes fid=6 but keeps any other parameters there.
I searched and found how to do it with one string but not two.
Also, what's the difference between redirect and rewrite?
This is related to MyBB forum software. I made a separate php file that uses forumdisplay but with a new name.
Using mod_rewrite you could use a condition to verify the id and grab what comes after if anything:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/forumdisplay.php
RewriteCond %{QUERY_STRING} ^fid=6(&.*|.*)
RewriteRule ^.*$ /blog.php?%1 [R=301,L]

Mod rewrite to redirect except on a certain page

Trying to write a rewrite rule in my htaccess so that any request to /en/shop/index.html or /fr/shop/index.html stays on the server, but if the user goes to any other page it redirects to a different server. Here's what I've got so far and it doesn't work.
RewriteRule ^(.*)/shop/(.*) [L]
RewriteRule ^(.*)$ http://www.newwebsite.com/$1 [R=301]
Add a dash to tell the first RewriteRule that you want the matches to be passed through unchanged:
RewriteRule ^.*/shop(/.*)?$ - [L]
I also removed the first set of parentheses since you're not using the results of the match so there's no need to store the matched patterns. I assumed you might need to match /shop without a trailing slash so that's why the (/.*)? section is there.

Apache / Linux - .htaccess how to get specific variable from a query / url and apply to rewrite

I have a rule that works for one "direction" but, not the other.
A typical incoming url / query would be: (long url)
http://somedomain.com/getme.pl?dothis=display&partnum=1234567 (could be up to 9 digits)
I have this rule in place in my htaccess file:
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L]
Which works great for a bit of ease getting one of the unique part numbers:
http://somedomain.com/1234567.
However, I would like to make the long url "pretty" so, I assumed I could reverse(ish) it.
So, when a link on the site is clicked on (the long url) the htaccess file would process the long url to the beautified version.
I tried MANY attempts.
Here was my latest failure.
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L] #(works)
RewriteCond %{QUERY_STRING} ^partnum=([0-9]*) #(tried to get partnum)
RewriteRule ^.* http://%{HTTP_HOST}/%1 [R] #(make the short url)
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L] #(the known working rule)
I have tried a plethora of rules and visited many sites for advice.
I tried with just rules, just conditions and variations of query_string.
So, I believe I must just grab the "partnum" from the query and rewrite to /1234567 or http_host/1234567
Then, allow the other rule (works) to process.
So BOTH:
http://somedomain.com/getme.pl?dothis=display&partnum=1234567
and
http://somedomain.com/1234567
Display as: http://somedomain.com/1234567 in the browser.
and both passed the whole query to the getme.pl script properly.
I found some close answers here but, none that really explained what I needed.
Can someone please help?
From the sounds of it, this should get you moving down the right path:
# Your working rewrite, with extra param on the rewrite
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1&rewrite [L]
# Redirect for long urls, to pretty url
# -The appended '&rewrite' on the first rule will force this not to match
# -The trailing '?' on the rewrite will strip the query string
RewriteCond %{QUERY_STRING} partnum=([0-9]*)$
RewriteRule (.*) /%1? [L,R=301]
Hope that helps.

Resources