RewriteRule to rewrite /folder/subfolder/file to /?f=folder/subfolder/file? - .htaccess

Pretty straightforward - just wondering how do I rewrite this:
/folder/subfolder/file
/?f=folder/subfolder/file
And the number of folders/subfolders might vary, so what I really want is anything after the / in that path (that does not already have ?f= in it) redirected to the path with ?f= added in front of it.
If I add this
RewriteRule ^(.*)$ ?f=$1 [L]
it works just fine but doesn't recognize the existence of ?f= in the URL already, and breaks that existing link with ?f= in there.
Any ideas?

You can use:
RewriteCond %{QUERY_STRING} !(?:^|&)f= [NC]
RewriteRule ^(.*)$ ?f=$1 [L,QSA]
Which does not do the rewriting if the f querystring is already there.
With [QSA] you keep the other possible querystring values.

Try this :
RewriteEngine On
RewriteRule ^(.[^\?]*)$ /?f=$1 [L]

Related

URL rewriting via my .htaccess file

How do I change example.com/1 to example.com/?id=1
I've tried googling but I can only find code for example.com/?id=1 to example.com/1
I used a generator and got this, but it didn't work
RewriteEngine On
RewriteRule ^\?get\=$ / [L]
Thanks,
Isaac
It kind of depends on which way you want to do the rewrite - ie, what does the user type see, and what does the server do.
If you want the user to see "http://example.com/1" and internally the server provides "http://example.com/?id=1", then the following should work:
RewriteRule ^([0-9]+)$ /?id=$1
However, if you want the user see "http://example.com/?id=1", and internally the server provides "http://example.com/1", then the following, as per Jon Lin's answer, should do it:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^$ /%1?
You can't match against the query string in a RewriteRule statement, you need to use a RewriteCond and the %{QUERY_STRING} variable:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^$ /%1? [L]
The ? is needed in the rule's target to remove the query string.

What's wrong with my RewriteRule? It seems ok, but it doesn't work

I have a rewrite rule on my webpage.
RewriteEngine On
RewriteRule ^(.*) index.php?p=$1 [L]
I want it to work so it rewrites URLs like this:
http://example.com -> index.php
http://example.com/home -> index.php?p=home
http://example.com/lol -> index.php?p=lol
But when I use the following php code inside my index.php
print_r($_GET)
It gives this:
Array ( [p] => index.php )
It gives this same results in all URLs (I tried these: http://example.com, http://example.com/, http://example.com/about, http://example.com/about/
Can you help me debig this?
I got it figured out:
The correct code is this:
RewriteEngine On
RewriteRule ^([^.]+)/?$ index.php?p=$1 [NC,L]
Sorry for the question.
The issue is that your rewritten URL still matches the rule and you get a new rewrite:
http://example.com/home
http://example.com/index.php?p=home
http://example.com/index.php?p=index.php
Since the [QSA] flag is not set, the new p parameter replaces the previous one. (I'm not fully sure about why you don't get an infinite loop, I suppose mod_rewrite makes a check to avoid useless redirects).
You need to add additional conditions. For instance, you can make a rewrite only if the URL does not match a physical file or directory:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [L]

Use a different RewriteRule depending on whether or not QueryStrings are used?

On my blog (which I'm in the process of redesigning for a more efficient way of displaying content), I have a RewriteRule setup to handle a simple redirection from
http://new.kn3rdmeister.com/blog.php
to
http://new.kn3rdmeister.com/category/blog/ (and it works backwards too)
In the .htaccess:
RewriteEngine On
RewriteRule ^blog\.php$ /category/blog/ [R=301,L]
RewriteRule ^category/blog/?$ blog.php [L]
...
That works just dandy. Now what I would like to do is rewrite
/blog.php?pagenum=x
to
/category/blog/page/x/
In the .htaccess:
...
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ blog\.php\?pagenum=([0-9]*)\ HTTP/
RewriteRule ^blog\.php$ /category/blog/page/%2/ [R=301,L]
RewriteRule ^category/blog/page/([0-9]+)/$ blog.php [L]
All I can get to work is the first rule, which ends up working, but it displays the QueryStrings rather than the oh-so-clean page directories, like so...
/blog.php?pagenum=2 for example equals
/category/blog/?pagenum=2
I hope I've made my question clear enough :/ If you need anything clarified, please ask and I'll update my post. Thanks in advance.
Add a question mark to the end of your target in your RewriteRule:
RewriteRule ^blog\.php$ /category/blog/page/%2/? [R=301,L]

RewriteRule [L] still changing the url

I found myself with this problem, which is driving me a little bit crazy. I use apache's mod_rewrite for pretty URLs and I need to use dynamic subdomains in the site. Everything is great and all the server has de wildcards. I use the next code on my .htacess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.com
RewriteCond %{HTTP_HOST} ([^.]+).mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/%1 [L]
The only problem is, even if I use the [L] flag the url of the site change to http://mysite.com/subdomain. What i want is the url to be like http://subdomain.mysite.com
The link mysite.com/subdomain is a dynamic url and is solved with another rule with the following code:
RewriteRule ^([A-Za-z]+)$ filter.php?type=subdomain&subdomain=$1
Any help would be appreciated
If you specify an external URL (which changing the subdomain does), a header redirect will take place. I don't think you can prevent that. But why not skip that step altogether, and use the second RewriteRule straight away?
I can't test this right now, but something like
RewriteCond %{HTTP_HOST} !^www.mysite.com
RewriteCond %{HTTP_HOST} ([^.]+).mysite.com [NC]
RewriteRule ^(.*)$ filter.php?type=subdomain&subdomain=$1
should work.

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