.htaccess Get requests in the URI - apache

I don't know how I would word this, and I can't make sense of the docs for httpd so I was wondering if anyone knew how to do this.
I would like to get
www.example.com/v/12345/yadayada.png
to actually go to
www.example.com/view?key=12345&img=yadayada.png
I've seen this done some websites but I cant find out how to do this.
Thanks
Edit 1:
I tried this, and then entered the following URL:
www.example.com/v/3f210a2c76cb100f4f7fbd7691a9eb967cb7a1a7/10b78802581bfd59f3fe2b447575bdf7.png
When I did this I got the following error:
The requested URL /v/3f210a2c76cb100f4f7fbd7691a9eb967cb7a1a7/10b78802581bfd59f3fe2b447575bdf7.png was not found on this server.
This is my current .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^./]+\.png)$ /i/$1 [L,NC]
RewriteRule ^v/([0-9]+)/([^/.]+\.(png|jpe?g|gif))$ /view.php?k=$1&img=$2 [L]

#JonLin had the right rule, but the rewrite rule you have declared before that one,
RewriteRule ^([^./]+\.png)$ /i/$1 [L,NC]
is applied to the example url you provided. So, with a request like
www.example.com/v/3f210/10b7.png
would get rewritten to
www.example.com/i/v/3f210/10b7.png
It's worth noting the Flags used for your rules
L - Stop the rewriting process immediately and don't apply any more rules
NC - Makes the pattern comparison case-insensitive.
If you were to remove that rule you would get the results you want. You should also determine if that rule is needed and modify it, the flags, and/or rearrange the order of your rules.
RewriteEngine On
RewriteRule ^v/([0-9]+)/([^/.]+\.(png|jpe?g|gif))$ /view.php?k=$1&img=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Try these rules in the htaccess file in your document root:
RewriteEngine On
RewriteRule ^v/([0-9a-f]+)/([^/.]+\.(png|jpe?g|gif))$ /view?key=$1&img=$2 [L]

Related

How to I replace a part of the URL string containing a query using htaccess?

I have a URL string using a PHP query string.
I want to make the URL pretty, but I haven't been able to make the rule properly.
Currently the URL looks like this:
http://localhost/pages/map?name=Skyfall-2022
But I want it to look like this:
http://localhost/pages/Skyfall-2022
This is my current htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^pages/(.+)$ /pages/map?name=$1
I get a 500 Internal Error with this when I try to type in the desired URL. I don't really understand Regular Expressions so I would appreciate if someone could adjust this for me with an updated .htacces
With your shown samples, please try following htaccess rules.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect for url change in browser rules here...
RewriteCond %{THE_REQUEST} \s/(pages)/map/?\?name=(\S+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
##Internal rewrite here for internal file's serving here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ $1/all-maps.php?name=$2 [QSA,L]
NOTE: I have mentioned map? in rewrite rules(2nd set of rules written in comments also in rules), you can change it with your php file's name whatever php file you have to pass query string to.
NOTE2(OP's fixes as per OP environment): tweaked 1 rule a
bit to $1/all-maps.php?name=$2 to $1/map.php?name=$2 and moved the all-maps.php one directory down and these rules worked fine, mentioned by OP in comments here. Just sharing here, in future it could help people that apart from above rules this was done as part of solution.

Mask URL rewrite in htaccess

I have a wordpress website in project and I want to mask the URL of all the pages so that when accessing them:
https://myweb.com/survey/page1
https://myweb.com/survey/page2
....
is displayed as:
https://myweb.com/survey/portal
I have this on .htaccess but it doesn't work:
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /survey/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /survey/index.php [L]
RewriteRule ^survey/?$ /survey/portal/
Thank you all for your time.
Very nice efforts first of all, could you please try following; written based on your shown samples. Please clear your browser cache before testing your URLs.
Also please your .htaccess file just one level above survey folder.
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /survey/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://%{HTTP_HOST}/portal [R=301]
RewriteRule ^ survey/index.php [L]
There is a common misconception that rewrite rules make ugly URLs pretty; it is more correct to say that they make externally pretty URLs internally ugly.
That's because when the browser sends a request to the web server, the web server can decide what response to serve, but it can't change what the browser sent - that's already happened.
So if you type https://myweb.com/survey/portal into a web browser, the URL sent to the server at myweb.com will be /survey/portal. Your rewrite rules decide what to do when receiving that URL, so your rule might look like this:
RewriteRule ^survey/portal$ survey/index.php
On the left is the URL the browser sent; on the right is what to serve. But notice that it wouldn't make sense to write this:
RewriteRule ^survey/portal$ survey/page1.php
RewriteRule ^survey/portal$ survey/page2.php
RewriteRule ^survey/portal$ survey/page3.php
These all match the same URL, and that URL is all we have to go on, so there is no way to map one URL to multiple pages using this mechanism. You would need something somewhere else to know which page of the survey the user is on.
You can of course do the opposite - match multiple URLs in the browser to the same resource internally:
RewriteRule ^survey/page1$ survey/index.php
RewriteRule ^survey/page2$ survey/index.php
RewriteRule ^survey/page3$ survey/index.php
Or you can map them to slightly different resources:
RewriteRule ^survey/page1$ survey/index.php?page=1
RewriteRule ^survey/page2$ survey/index.php?page=2
RewriteRule ^survey/page3$ survey/index.php?page=3
And you can use patterns and placeholders to avoid having to list out all the possibilities, so that last example can be shortened to:
RewriteRule ^survey/page([1-3])$ survey/index.php?page=$1

Can't get mod_rewrite to correctly (and invisibly) re-write my URLs?

I'm trying to make a URL shortening service for my website.
So instead of:
http://www.myfullwebsitename.com/page78/this-is-a-headline/
users will be able to visit:
http://abc.de/aBxf
which needs to redirect (invisibly!) to
http://abc.de/?shorturl=aBxf
which then 301 redirects via a database lookup to
http://www.myfullwebsitename.com/page78/this-is-a-headline/
I can do the DB lookup and the 301 redirect easily. It's the invisible intermediate redirect that I'm struggling with.
I've tried a LOT of different things, but none seems to work. This is what I currently feel should work:
RewriteCond %{HTTP_HOST} ^abc.de
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/(.+) /?shorturl=$1
But instead of redirecting silently to
http://abc.de/?shorturl=aBxF
it redirects "noisily" (302) to
http://abc.de/aBxF/?shorturl=aBxF
What am I doing wrong?
Thank you!
There's a few things you can try.
I think your RewriteRule should look like this (without the forward /):
RewriteRule ^/(.+) ?shorturl=$1 [L]
This should at the very least stop it from redirecting to http://abc.de/aBxF/.
Your original rule may work if you add:
RewriteBase /
If it were me my rules would actually look like this:
RewriteBase /
RewriteCond %{HTTP_HOST} ^abc.de$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /redirect.php [L]
And then in PHP I would use $_SERVER['REQUEST_URI'] to get the URL (not sure what language you're using).
The rule can look like this:
RewriteRule ^(.*)$ /redirect.php?shorturl=$1 [L]
But I would make sure to mention the script by name. Part of what may be throwing your rules off is relying on Apache finding your index file after a rewrite.
The way Apache's rewrite rules work is as soon as the URL is rewritten, it actually will re-run the rules until no other rules will be found. The [L] flag for "last" says "stop here" - but it still starts over from the top. The RewriteCond with the !-f flag says "only if the file doesn't exist".
Use an absolute URL:
RewriteCond %{HTTP_HOST} ^abc.de
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://abc.de/?shorturl=$1 [R=301,L]

.htaccess question - URL-rewriting

I have an issue with URL-rewriting in .htaccess. Here is .htaccess file:
RewriteEngine On
RewriteBase /community/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^view-all-results$ forums/index.php?view=view-all-results [R=302]
RewriteRule ^view-all-results/$ forums/index.php?view=view-all-results [R=302]
I need to rewrite url like "/community/view-all-results?u=2" to "community/forums/index.php?view=view-all-results&u=2".
But according to the above rule I'll get "community/forums/index.php?view=view-all-results".
I tried to change RewriteRule to
RewriteRule ^view-all-results?(.*)$ forums/index.php?view=view-all-results&$1 [R=302]
But it doesn't work properly. It still rewrites URL to "community/forums/index.php?view=view-all-results".
When I changed rule(put + instead of *):
RewriteRule ^view-all-results?(.+)$ forums/index.php?view=view-all-results&$1 [R=302]
I've got URL like "community/forums/index.php?view=view-all-results&s". So I don't understand this behavior.((
I will be very appreciated for any suggestions.
The magic flag is in the docs: [QSA], which will add the original querystring to your url.
Normal matching is only done against the path, not agains the querysting, which you would find in the magic variable %{QUERY_STRING}). Matching this variable can be done in a RewriteCond condition. You could also append this variable to the resulting url, but QSA is infinitely more userfriendely here.
Give this a try...
RewriteEngine On
RewriteBase /community/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^view-all-results/?$ forums/index.php?view=view-all-results [QSA]
Basically the first half of a RewriteRule doesn't match against the QUERY_STRING, so you second to example will never match against it. The main thing your first code was missing was the QSA flag, which tells it to pass the QUERY_STRING it receives along with the newly created QUERY_STRING. I also removed the R=302, as I assume you don't want the URL to change.
Edit: Oh, I also combined the rules by making the trailing slash optional.

Help with mod_rewrite rule for dynamic url

Ugh.. mod_rewrite makes me feel stupid. I just haven't wrapped my brain around it yet. :/
I have this url:
http://example.com/a/name/
...that I want to point here:
http://example.com/a/index.php?id=name
...where name is what is getting passed to index.php as the id argument.
Anything I've tried results in either a 404 or a 500.. :(
If you want the trailing slash to be optional, you have to exclude the file you are rewriting the request to. Otherwise you will have a nice infinite recursion.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/a/index\.php$
RewriteRule ^/a/([^/]+)/?$ /a/index.php?id=$1 [L]
Here any request that starts with /a/… but it not /a/index.php is rewritten to /a/index.php.
But if the trailing slash is mandatory, there is no need to exclude the destination file:
RewriteEngine on
RewriteRule ^/a/([^/]+)/$ /a/index.php?id=$1 [L]
To start you off:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^/?a/([^/]+)/?$ /a/index.php?id=$1 [QSA,L]
If one rewrite tutorial doesn't work for you, try another.
Edit: excluded index.php as per Gumbo's suggestion
Maybe something along the lines of
RewriteEngine on
RewriteBase /a/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [L,QSA]
would do the trick.
I suggest you take a look at this URL:
http://www.dracos.co.uk/code/apache-rewrite-problem/
The presented solutions will work, but there are some caveats explained in the URL, mainly regarding ? and # in the URLs themselves.