Replace all question mark and equal sign by slash? - .htaccess

Is there a way to replace all question mark, '&' , and equal sign by slash ???
currently I am only replacing php extensions by .html
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [nc]
now, if the websites url is this
http://www.mydomain.com/session/image-display.php?image=1&id=59
the user should get
http://www.mydomain.com/session/image-display.html/image/1/id/59
--edited---
I just need a example so that i could write them for other pages as well.
also how to rewrite 2 or more rewrite rules ??

Based on the example you gave, you can use this:
RewriteRule ^session/image-display.html/image/(.*)/id/(.*)
/session/image-display.php?image=$1&id=$2 [L]
The [L] at the end is optional, it tells Apache to stop looking for other rules if this one is applied

You need to change your links so that they look like:
http://www.mydomain.com/session/image-display.html/image/1/id/59
Then add this in the htaccess file in your document root, before any rules that you may already have:
RewriteCond %{THE_REQUEST} \ /session/image-display\.php\?image=([^&]+)&id=([^&\ ]+)
RewriteRule ^ /session/image-display.html/image/%1/id/%2? [L,R=301]
RewriteRule ^session/image-display\.html/image/([^/]+)/id/([^/]+) /session/image-display.php?image=$1&id=$2 [L]

Try this in .htaccess:
RewriteRule ^(.*)\.html/image/([^/]+)/id/([^/]+)$ $1.php?image=$2&id=$3 [NC]

These 2 rules will internally rewrite /session/image-display.html/n1/v1/n2/v2/n3/v3 to /session/image-display.php?n3=v3&n2=v2&n1=v1
There can be any number of /name/value pairs as this rule is pretty generic.
# this rule will be applied as many times as there are /name/value pairs in URI
# after /session/image-display.html
RewriteRule ^(session/image-display\.html)/([^/]*)/([^/]*)(.*)$ /$1/$4?$2=$3 [L,QSA,NC]
# this rule be applied in the last then all /name/value pairs have been converted
# into query string
RewriteRule ^(session/image-display)\.html/?$ /$1.php [L,NC]

Related

Need Assistance for this Htaccess Rewrite Rule

I have a problem with my .htaccess, a short explanation I would like to set http://example.com/newest on my website. However, it always redirects to http://example.com/postname. Where I just need the exact "newest" page. Here is my code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteBase /
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^/category/(.*)$ page.php?f=$1
RewriteRule ^/search/(.*)$ search.php?f=$1
RewriteRule ^(.*)/$ post.php?f=$1 <- If this is removed, my post htaccess will not work
RewriteRule ^newest/$ index.php?f=newest <- I want to execute this code
I really don't know what this is called, I have been looking for the whole stackoverflow but I did not get any answer. Please remain me if this is a duplicate question.
As Mohammed implied in comments, your directives are in the wrong order. The line above your "newest" rewrite is a catch-all and rewrites all requests, so the last line will never match.
http://example.com/newest
Note that your rules imply that your URLs should end in a trailing slash. So, you should be linking to http://example.com/newest/ (with a trailing slash), not http://example.com/newest, otherwise your users will get a lot of unnecessary redirects.
However, you appear to be under the belief that the RewriteCond directive applies to all the directives that follow. This is not the case. It only applies to the first RewriteCond directive. You also need some L flags to prevent further processing.
You also have a slash prefix on the "category" and "search" rewrite patterns, so these would never match in a .htaccess context.
Try something like the following instead:
RewriteEngine On
RewriteBase /
# Don't process the request further if it maps to an existing file
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Append trailing if omitted
# Although strictly speaking this only redirects if there are no slashes at all in the URL
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^category/(.*)$ page.php?f=$1 [L]
RewriteRule ^search/(.*)$ search.php?f=$1 [L]
RewriteRule ^newest/$ index.php?f=newest [L]
RewriteRule ^(.*)/$ post.php?f=$1 [L]

.htaccess rewrite and redirect two types of urls

I'm trying to create clean URL with .htacces rewrite. I have two types of urls:
site.com/page.php?page=something
site.com/something.php
I need them both to be just site.com/something, with redirect from ugly to pretty url. So now I have the following rules, which don't work together, and I totally stuck with the redirect.
Options -Multiviews
RewriteEngine On
RewriteBase /
# page.php?page=about to about
RewriteRule ^([^/.]+)?$ /page.php?page=$1 [NC,L]
# something.php to something
RewriteRule ^([^/.]+)?$ $1.php [NC,L]
Will appreciate any help. Thanks in advance.
There are a few things that are incorrect in your example:
You cannot inspect the query string in a RewriteRule, only in a RewriteCond
Your RewriteRule lines are backwards - the first part is a regular expression match of the URL and the second is what you want it to be.
You will need to have an [R] rule as part of the rewrite to perform a redirect, otherwise it will just "rewrite" the URL the server sees and not change the actual URL.
Here is an example of your first rewrite, redirecting /page.php?page=foo to /foo. You first need a RewriteCond to inspect the %{QUERY_STRING} variable to see if it has page=... in it. We can use the character match ([^&]*) to grab all of the characters that are not an ampersand and store in a matching group. Next we perform a RewriteRule for page.php (note that we don't need the leading / because of the RewriteBase and that the . is escaped). If there is a match here, you want to redirect to the matching group from the RewriteCond - it is referred to with a %1 rather than a $1 like it would if it were from the RewriteRule. You will also want to append a ? to the end of your redirect which tells Apache to drop the query string so you don't end up with /foo?page=foo. Finally you will need [R=301] to perform a redirect with an HTTP status code of 301. The [L] indicates that that this is the Last rule you want to process if there is a match.
RewriteEngine On
RewriteBase /
# page.php?page=about to about
RewriteCond %{QUERY_STRING} page=([^&]*) [NC]
RewriteRule page\.php /%1? [R=301,L]
Your second rewrite is closer, but as in the first the logic is backwards. You want the first part to match *.php and then the second to indicate the redirect to /$1. Again you will need the [R-301] for the redirect.
# something.php to something
RewriteRule (.*)\.php$ $1 [R=301,L]
You can test this out on http://htaccess.madewithlove.be/.
Using http://example.com/page.php?page=foo, redirects to http://example.com/foo
1 RewriteEngine On
2 RewriteBase /
3 # page.php?page=about to about
4 RewriteCond %{QUERY_STRING} page=([^&]*) [NC]
This condition was met
5 RewriteRule page\.php /%1? [R=301,L]
This rule was met, the new url is http://example.com/foo
Test are stopped, because of the R in your RewriteRule options.
A redirect will be made with status code 301
Using http://example.com/foo.php redirect to http://example.com/foo
1 RewriteEngine On
2 RewriteBase /
3 # page.php?page=about to about
4 RewriteCond %{QUERY_STRING} page=([^&]*) [NC]
This condition was not met
5 RewriteRule page\.php /%1? [R=301,L]
This rule was not met because one of the conditions was not met
6 # something.php to something
7 RewriteRule (.*)\.php$ /$1 [R=301,L]
This rule was met, the new url is http://example.com/foo
Test are stopped, because of the R in your RewriteRule options.
A redirect will be made with status code 301

SEO Url for Profiles

Preface
I'm trying to re-write a URL for a profile page. All of my application pages have a .html extension, so I'm trying to match just letters, numbers, -, and ..
So these would be valid
site.com/steve
site.com/steve-robbins
site.com/steve.robbins
But these wouldn't be
site.com/steve.html
site.com/steve-robbins.php
Assume I have a check in place so that custom URLs don't have .html or .php on the end.
Problem
I'm currently using this but it's not working
RewriteRule ^([a-zA-Z0-9\.-]+)$ profile.php?url=$1 [L]
It should set url to steve, but it's setting it to profile.php
What am I doing wrong?
My complete .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301]
#
# LOGIN
#
RewriteRule ^([a-z0-9]{255})/activate\.html$ login.php?activate=$1 [L]
RewriteRule ^logout\.html$ login.php?logout [L]
#
# SETTINGS
#
RewriteRule ^change-([a-z]+)\.html$ account-settings.php?$1 [L]
RewriteRule ^([a-zA-Z0-9\.-]+)$ profile.php?url=$1 [L]
# SEO friendly URLs
RewriteRule ^([a-zA-Z0-9-_.]+)\.html$ $1.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z0-9-_.]+)\.php
RewriteRule ^([a-zA-Z0-9-_.]+)\.php$ $1.html [R=301]
Add this to the top of your rules (under the RewriteBase / directive):
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
That should stop it from looping. The rewrite engine will keep re-applying all the rules until the URI going in (sans query string) is the same as the URI that comes out of the rules. That's why the value of url is profile.php.
I'm kind of a beginner in interpreting mod_rewrite rules but if I understand it correctly your rule is matched and than matched again, either add something to the url matching scheme like /profile/user or add a condition to not redirect if already redirected
Try adding a leading slash to the redirect like this:
RewriteRule ^([a-zA-Z0-9.-]+)$ /profile.php?url=$1 [L]
The reason you're getting a url value of profile.php is because the [L] flag is kinda misleading when it comes to the .htaccess file. In the server config files it does exactly what you'd think, but in the .htaccess file it stops reading rules at that rule, but then goes through the rules again until path is unchanged by any of the rules. By adding the leading /, your rule will not match the second time around as you exclude / from the regex. I spent a while struggling with this feature myself.

How do I map path components into query parameters using mod_rewrite?

How would I change this:
http://www.mattvisk.com/?page=portfolio&item=rae
To this:
http://www.mattvisk.com/portfolio/rae
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(images/|css/|js/)
RewriteRule . - [S=2]
RewriteRule ^([a-z0-9_\-\.]+)/?$ index.php?page=$1 [L,NC,QSA]
RewriteRule ^([a-z0-9_\-\.]+)/([a-z0-9_\-\.]+)/?$ index.php?page=$1&item=$2 [L,NC,QSA]
Make sure mod_rewrite is enable
edit: I added the dot, poolie had a good point there.
The [S] flag is used to skip rules that you don't want to run. This can be thought of as a goto statement in your rewrite ruleset. In the following example, we only want to run the RewriteRule if the requested URI doesn't correspond with an actual file.

301 Redirect with .htaccess

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^category?$
RewriteRule ([^/]+)/([^/]+)/([^/]+)/$ http://domain.com/$3/ [L,R=permanent]
Currently I have the following redirection and it is working like a charm. Now I want to make sure that the link does not begins with /category/ therefore I have inserted the condition. Unfortunately it does not seems to work. Please help. Thanks.
Another question is, how to make that the end permalink that is between the slash is selected to be redirected only. For example, I may have links like http://domain.com/downloads/26-fine-wallpapers/ and http://domain.com/downloads/icons/35-nice-icons/ and I want links like these to be redirected to http://newdomain.com/35-nice-icons/ and http://newdomain.com/26-fine-wallpapers/
I am using wordpress actually.
According to your description you only have two path segments. So your pattern should be:
RewriteRule ^([^/]+)/([^/]+)/$ http://example.com/$3/ [L,R=permanent]
And to exclude /category/…, you can either check the request URI path in REQUEST_URI:
RewriteCond %{REQUEST_URI} !^/category/
RewriteRule ^([^/]+)/([^/]+)/$ http://example.com/$3/ [L,R=permanent]
Or you check the matched value of the first group:
RewriteCond $1 !=category
RewriteRule ^([^/]+)/([^/]+)/$ http://example.com/$3/ [L,R=permanent]
I think you just need a prefixing /:
RewriteCond %{REQUEST_URI} !^/category?$

Resources