Using part of url as gettable variable, not a wildcard - .htaccess

I am trying to $_GET part of a rewritten url but it only seems to work when I use a wildcard (.*).
So this works for me:
RewriteRule ^info/(.*)$ _extern/web/listing.php?alias=$1 [QSA,L]
But this doesn't :
RewriteRule ^winkels$ _extern/web/listing.php?alias=$1 [QSA,L]
On info/winkels I can retrieve the alias and it shows 'winkels' when I echo it, but on /winkels it's empty.
Why?

You forgot to actually capture the part of the URL you want to hand over.
Have a try using that rewrite rule:
RewriteRule ^/?(winkels)$ _extern/web/listing.php?alias=$1 [QSA,L]
Note: without the capturing brackets $1 will simply be empty...

Related

.htaccess rewrite is not working

I want to change the URL of my site:
http://example.com/clinicdetails.php?url=/diet-clinic-in-punjabi-bagh.html
to look like this:
http://example.com/clinicdetails.php/diet-clinic-in-punjabi-bagh.html
My code in .htaccess file is as follows:
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /clinicdetails.php?url=$1 [L]
However, this is not working (no error) on my localhost and also not on the shared hosting servers. What could be the issue here?
RewriteRule ^([^/]*)\.html$ /clinicdetails.php?url=$1 [L]
The RewriteRule pattern is failing to match the given URL because of the start of string anchor ^. Whilst you don't have slashes in the path segment you are trying to match, you do in the rest of the URL. So, try the following:
RewriteRule ([^/]+)\.html$ /clinicdetails.php?url=/$1 [L]
I've also changed the * to +, since the filename is at least 1 char. A more specific regex is better.
To map to your desired URL, you would also seem to require a slash prefix on the URL param value. ie. url=/$1

URL Rewriting not possible?

I have a website with this url :
http://www.example.com/stage?dept=01
I want transform this url to this
http://www.example.com/stage-ain.html
I want that new url override the standard html and it's become the only url available (for SEO).
I do this, but it's not ok :
RewriteEngine On
RewriteRule ^stage?dept=01$ stages-ain.html [R=301]
Have you an idea ?
Thanks
The first argument of RewriteRule cannot match the query string. You have written a regex instead that matches the url stagedept=01 and stagdept=01.
You want to use a RewriteCond instead. You could use the %{QUERY_STRING} variable, but this will likely cause an infinite loop. Instead you probably want to match on %{THE_REQUEST}.
RewriteCond %{THE_REQUEST} /stage\?dept=01
RewriteRule ^ stages-ain.html [R,L,QSD]
Change the [R] flag to [R=301] when you have tested everything works as expected. Always use the L flag with external redirects, unless you have a very good reason to continue rewriting, as this can cause some weird problems.
See the documentation for more information.

How to write a htaccess to change url but point to actual path internally using regex match

url = www.example.com/de/abc
I just want to change fix url word 'abc' into fix word 'xyz' for browser compatibility only changed url show in browser. But change url point to actual directory path means
www.example.com/de/xyz will follow internally www.example.com/de/abc.
I used some regex to do this, but not able to get actual solution
How can I do this.
Thanks in advance
Code from comment :
RewriteCond %{REQUEST_URI} ^/de/advertiseWithUs$ [NC]
RewriteRule ^(.*) /de/unsere-tipps [R=302,L]
If I have correctly understood what you want, here a solution you can try :
RewriteRule ^de/advertiseWithUs$ http://yourdomain.com/de/unsere-tipps [R=302,NC,L]
RewriteRule ^de/unsere-tipps$ de/advertiseWithUs [NC,L]
The first rule change URL (with a 302 redirection), the second one make the internal redirection to point to the right page.

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]

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.

Resources