htaccess: Mediafire.com like urls - .htaccess

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]

Related

.htaccess redirect question mark with no parameters

I have several URLs with question marks that need to be removed. For example, I need to redirect this URL:
http://example.net/?/services
To this URL:
http://example.net/services
I have many more like this, so I would like something that can catch everything with the question mark and properly redirect it. Many of the answers I found were trying to use QUERY_STRING as the condition for the rewrite, but without parameters this does not help. After some digging I found a RewiteCond that works, but the RewriteRule redirects to the homepage, rather than the URL without the question mark. What I have currently is this:
# Remove question mark from string
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
The first block is the rewrite that I have so far, and the next two are for the CMS url routing. What seems to be happening is that my rewrite in the first block is not keeping the rest of the url. I have tried several combinations and can't seem to figure out how to keep the rest of the url intact.
Many of the answers I found were trying to use QUERY_STRING as the condition for the rewrite, but without parameters this does not help.
Yes, this is exactly what the first URL, with a question mark, contains. So, I'm not sure why "this does not help"? In the URL http://example.net/?/services, /services is the query string. Whether there are key/value pairs (ie. "parameters") is irrelevant.
To redirect URLs of the form http://example.net/?/services, that consist of no URL-path and only a query string, try something like:
# Remove question mark from string
RewriteCond %{QUERY_STRING} ^/(.+)
RewriteRule ^$ /%1? [R,L]
%1 is a backreference to the captured group in the last matched CondPattern (ie. (.+), which captures services). This assumes that the query string (after the ?) always starts with a slash, as in your example. (Incidentally, this is also what your front controller is doing, in reverse, so I assume it must be correct.)
The trailing ? on the substitution removes the original query string from the request.
Make sure you clear your browser cache, as any earlier/erroneous 301s will have been cached by the browser.
If this is intended to be a permanent (301) redirect then change R to R=301, but only when you are sure it's working OK.

Remove certain part of URL

I have a website where i have pages like:
domain.com/?p=settings
domain.com/?p=daily
And i am looking for rewrite that ?p= part, so it would be like
domain.com/settings
So far i have tried to add this to htaccess files:
RewriteRule ^([^\.]+)$ $1?p= [NC,L]
but it did not worked.
Also I have tried look from Google but could not find any.
I have tried other RewriteRule's but they did not work either.
RewriteRule does not include query string. It is available as a separate variable enter link description here
The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html"). This is the (%-decoded) URL-path.
So the following won't work.
RewriteRule ^([^.]+)$ $1?p= [NC,L]
You need something like
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/ %2?
Checkout Apache Mod ReWrite Wiki and scroll down to "Making the Query String Part of the Path"
You were close with your attempt, you need to use this in your .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /?p=$1 [L]
Make sure to clear your cache before testing this.

htaccess file seems to pick up 'if contains' for a RewriteRule

We currently have a .htaccess RewriteRule that's incorrectly (or correctly as the rule is incorrect) redirecting a URL.
The Rule
RewriteRule ^holiday-ecards/?.*$ /appindex.php [L]
The desired redirects for this are:
http://domain.com/holiday-ecards/
http://domain.com/holiday-ecards/1/
http://domain.com/holiday-ecards/1/2
http://domain.com/holiday-ecards/1/2/3
However, it seems to also be redirecting the following, which is undesired:
http://domain.com/holiday-ecards-business/
EDIT
/appindex.php
This is taking care of the app routing and works as intended.
A number of ways you could do it, one would be setting a rewrite condition to not touch URI's that have holiday-ecards plus hyphen, like so:
RewriteCond %{REQUEST_URI} !^/holiday-ecards-.*$
RewriteRule ^holiday-ecards/?.*$ /appindex.php [L]
Not sure how many variations you have of URI's with holiday-ecards in them.
RewriteRule ^holiday-ecards/?.*$ /appindex.php [L]
(Note that this is an internal rewrite, not a redirect.)
The above RewriteRule pattern makes the slash after holiday-ecards optional (so it will also match holiday-ecards-business). However, in the example URLs that should be rewritten, the slash is mandatory. So, it would appear that you just need to make it mandatory (?), for example:
RewriteRule ^holiday-ecards/ /appindex.php [L]
The trailing pattern .*$ is superfluous.

htaccess rewrite rewrite url with folders after php file

i am trying to get something like this:
http://localhost/Engine/admin.php/users/manage
to rewrite to something like this:
http://localhost/Engine/admin.php?p=users&a=manage
i am a beginner with rewrite rules, but i have a basic one here, they cant conflict...
RewriteEngine on
RewriteBase /
RewriteRule ^([^.]*)$ /Engine/index.php?page=$1 [QSA,L]
also, if anyone has any improvements for my other rewrite, it would be most helpfull, it rewrites
http://localhost/Engine/somepage
to:
http://localhost/Engine/index.php?page=somepage
Your generic rule that matches everything should be listed as last rule. You can improve it by only allowing to match if .php is not in the url. For admin.php you know how many arguments there should be, so you can simply match the static part of the url with ^Engine/admin\.php/, then all arguments with ([^/]+) (match everything but /). The last /? matches a trailing slash that might or might not be appended.
For more information read the documentation.
RewriteRule ^Engine/admin\.php/([^/]+)/([^/]+)/?$ /Engine/admin.php?p=$1&a=$2 [L]
RewriteCond %{REQUEST_URI} !^/Engine/.*\.php.*$
RewriteRule ^Engine/(.*)$ /Engine/index.php?page=$1 [QSA,L]

htaccess rewrite querystring and remove empty value

first, sorry for my bad English.
I try to rewrite url generated from Form Get and redirect that.
my url is like this:
http://www.mysite.com/properties?action=search&agreement=for-rent&category=my-category&type=&zone=my-zone&city=my-city
and I have this .htaccess configured:
11. RewriteCond %{QUERY_STRING} ^action=(?:[a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)$
12. RewriteRule (.*) %{REQUEST_URI}/%1/%2/%3/%4/%5/? [R=301,L]
So basically all my request are direct to index.php.
21. RewriteCond %{REQUEST_URI} !index\.php|resources|hidden
22. RewriteRule ^(.*)$ index.php/$1 [L]
All works, but the problem is when I have an empty value in query string, the rule add double slash and the above url (for example whit &type=&zone=my-zone... type have empty value) will translate like that:
http://www.mysite.com/for-rent/my-category//my-zone/my-city/
The question is: How can i remove in .htaccess the double slash generated if i have one or more empty value in query string?
Thanks
Easiest is to do another redirect (not real pretty as it requires two 301's).
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R=301,L]
The fun part is that when the url is loaded with a double slash in it, mod_rewrite will automatically remove this. So as you can see above you'll just have to rewrite the url to itself, kind of.

Resources