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
Related
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.
I'm pretty green when it comes to rewriting URL's with htaccess, though I can do the basics.
In this case I have a series of query strings returned from a vendor to one of my scripts. That's all fine and dandy, except for when it includes a URL with 'http' or 'https' in it. When it detects that, Apache throws a 403 Forbidden error. I thought that I could craft a RewriteRule that would rewrite the 'http' portion of the query string into something could get past Apache's rules.
This will eventually be installed on a client's machine so I can't change any server settings.
An example URL would be:
http://mysite.com/gocardless_confirm.php?resource_uri=https%3A%2F%2Fsandbox.gocardless.com%2Fapi%2Fv1%2Fbills%2F07F56ERHRT
Here's the settings I was trying to use:
RewriteCond ${QUERY_STRING} ^(.*)$resource_uri=http^(.*)$
RewriteCond %{REQUEST_URI} ^gocardless_confirm.php
RewriteRule ^(.*)$ gocardless_confirm.php?$1resource_uri=hllp$2 [L]
How can I rewrite this portion so I can simply get to the script?
Thanks!
Try:
RewriteCond %{QUERY_STRING} ^(.*)resource_uri=http(.*)$
RewriteRule ^gocardless_confirm.php$ gocardless_confirm.php?%1resource_uri=hllp%2 [L]
Subpattern in RewriteCond are referenced %# in the RewriteRule.
^ is the begin of the hole string the pattern is matched against.
$ is matches its end.
Basically, I've been trying to make some friendly URL's via .htaccess using mod_rewrite - and I've managed to get it to work... but only with basic stuff like:
RewriteEngine On
RewriteBase /
RewriteRule ^profile.php http://www.google.co.uk [L]
So mod_rewrite works, and I can re-direct to other sites, other files/directories in my server, etc. - but it seems to not work when I use this:
RewriteEngine On
RewriteBase /
RewriteRule ^profile.php?user=$1 ^profile/user/([^/]*)/$ [L]
Any help on this would be great, as I pretty much suck at mod_rewrite, but it's something I need to learn.
Cheers!
Change your [L] to [R,L] to force an actual HTTP redirect. Otherwise it just does the rewriting internally (when possible), which only affects the mapping from the URI to the filesystem. (See the description of the [R] flag at http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteflags.)
Wrong.
## rewriting from to
RewriteRule ^profile.php?user=$1 ^profile/user/([^/]*)/$ [L]
Should be
## rewriting from to
RewriteRule ^profile/user/([^/]+)$ profile.php?user=$1 [L]
Your configuration currently is this:
RewriteEngine On
RewriteBase /
RewriteRule ^profile.php?user=$1 ^profile/user/([^/]*)/$ [L]
In the RewriteRule you swapped the from and to parameters.
Assuming that on your server there is a directory structure like this:
/var/www/htdocs/profile/user/albert
/var/www/htdocs/profile/user/bob
Then you can use the following rule:
RewriteCond ${QUERY_STRING} ^user=(\w+)$
RewriteRule ^profile\.php$ profile/user/%1 [L]
There are some points that you got wrong here:
The request to "/profile.php?user=bob" first gets split into the Request URI and the Query String. Only the Request URI will be used by mod_rewrite. Therefore you have to handle the query string separately.
I restricted the user name to only [A-Za-z0-9_]. If I had allowed all characters, an attacker could easily call /profile.php?user=../../config.php, which would be rewritten to profile/user/../../config.php, and you probably don't want to share that file with the world.
The arguments to the RewriteRule directive are completely different regarding their syntax.
The first argument (the from part) is a regular expression, which usually starts with a caret ^ and ends with a dollar $.
The second argument (the to part) is the replacement, which is almost only a simple string, with only some special features. This string usually doesn't start with a caret, but looks rather like a pathname.
I'm trying to come up with some mod_rewrite to translate http://example.com/?7gudznrxdnu into http://example.com/view.php?id=7gudznrxdnu
But any other page will function properly such as http://example.com/contact and so on.
I think this will work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^[a-z0-9]+$
RewriteRule ^$ view.php?id=%{QUERY_STRING} [L]
If you want the rewrite to be shown in the browser's address field, you'll have to replace [L] with [L,R=301].
Explanation: The query-string (what's following the question mark) is not part of the URL that RewriteRule sees in its matching-pattern, therefore you can't check for question mark there. In my solution, I run the rule if and only if (RewriteCond) the query string consists solely of a-z and/or 0-9, and my rule only rewrites URLs ending with a slash (except for the query string). I redirect this to view.php?id=, and then append the query string to that.
Edit: Tested on my Apache-server, and I haven't found any bugs (yet).
You should try (in your .htaccess):
RewriteEngine On
RewriteRule ^\?([^/\.]+)?$ view.php?id=$1 [L]
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.