if url not exist htaccess rewrite - .htaccess

I'm newbie in htaccess..
I have some conditions like this..
if user access
first/second/third
it's equal to
first.api.php?rquest=second&attr=third
but if the value of rquest is 'index', it will be erased from URL, like
first/third
equal to
first.api.php?rquest=index&attr=third
not
first/index/third
I had all night looking for solution, but no result.
I know system being confused if the request is index but attr has a value, it will treat the attr like rquest. see above, first/third, system will treat the 'third' segment as rquest.
Is it possible to rewrite url like that?
this is my work all night, I know this is still crap..
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s
RewriteCond %{QUERY_STRING} !rquest=index
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ $1.api.php?rquest=$2&attr=$3 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/(.*)$ $1.api.php?rquest=$2 [QSA]
RewriteCond %{REQUEST_FILENAME} -s
RewriteCond %{QUERY_STRING} rquest=index
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ $1.api.php?rquest=index&attr=$2 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)$ $1.api.php?rquest=index [QSA,L]
</IfModule>

Try something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ /$1.api.php?rquest=$2&attr=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ /$1.api.php?rquest=index&attr=$2 [L,QSA]
Not clear what you were trying to achieve with your old rewrite rules, but the above should do what you've described.

Related

how to make friendly url using htaccess

I have been trying to force remove some unnecessary part of my url for example remove index.php? from this url.
/index.php?task=boost,boosting&action=index
The following rules apply not helped
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
how should look like the correct rule to hide unnecessary parts such as index.php or task or action?
With below rule you can use url yourdomain.com/boost/index you have to still pass value to task & action in some form so you cannot hide all the string from url until it is a post request.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)$ index.php?task=$1&action=$2 [QSA,NC,L]
Description You may use the following
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Remove duplicate content issue using htaccess

My .htaccess file looks likes this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?pg=$1 [L]
When a non-file or non-dir url is requested, it works fine.
But if index.php?pg=(pagename) is requested, it also works which can possibly lead to duplicate content issue.
Is there a way to prevent this?
You're right, this can lead to duplicate content.
And, yes, it is possible to avoid it.
This solution redirects old url to new url equivalent, for instance http://website.com/index.php?pg=get/my/page/please.html to http://website.com/get/my/page/please.html.
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/index\.php\?pg=([^&\s]*)\s [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?pg=$1 [L]

Redirect url get variables after rewriting htaccess

My original url was this:
wwww.domain.com/car-details.php?merk=BMW&model=X5&titel=sale&car_id=3
I have Rewrite it into this:
wwww.domain.com/BMW/X5/sale/3
By putting this in my .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)/([0-9]+)?$ /car-details.php?merk=$1&model=$2&titel=$3&car_id=$4 [L,QSA]
When i put in Url wwww.domain.com/car-details.php?merk=BMW&model=X5&titel=sale&car_id=3 it still works. I want that url to be redirected to wwww.domain.com/BMW/X5/sale/3
I have tried to put the [R] flag in my htaccess RewriteRule but then I'm getting the opposite results.
How can I fix this?
This is a good decision to do what you want.
But you will face a loop problem.
You can avoid it by using this code
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/car-details\.php\?merk=([^&]+)&model=([^&]+)&titel=([^&]+)&car_id=([0-9]+) [NC]
RewriteRule . /%1/%2/%3/%4? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([0-9]+)?$ /car-details.php?merk=$1&model=$2&titel=$3&car_id=$4 [L]

.htaccess single variable url rewrite

I am trying to redirect http://test.pearlsquirrel.com/explore_all?u=hiphop to http://test.pearlsquirrel.com/explore_all/hiphop using htaccess. However, It keeps redirecting here: http://test.pearlsquirrel.com/explore_all/?u=site_error.php. I have looked all over Stackoverflow for the answer but I just can't seem to find it. I would really appreciate some help in fixing this problem, thanks!
Here is my .htaccess file
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.pearlsquirrel.com$ [NC]
RewriteRule ^(.*)$ http://pearlsquirrel.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) explore_all?u=$1 [L]
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} [^=]+=([^=]+)
RewriteRule .* /%1? [L]
Will map this:
http://test.pearlsquirrel.com/explore_all?u=hiphop
To this:
http://test.pearlsquirrel.com/explore_all/hiphop
Replace the corresponding code in your .htaccess file with this one.
I have not checked the rules in your .htaccess file.
OPTION
Now, if it is the other way around, which I think it is, and the idea is to map this incoming URL:
http://test.pearlsquirrel.com/explore_all/hiphop
To this resource:
http://test.pearlsquirrel.com/explore_all?u=hiphop
Replace the above rule set with this one:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .*/([^/]+)/?
RewriteRule .* http://test.pearlsquirrel.com/explore_all?u=%1 [L]

htaccess mod rewrite - how to create virtual subdomains?

I would like to create virtual subdomains through htaccess in the following way.
Entering:
http://testuser.domain.com/1/2/3/
Should be processed as:
http://www.domain.com/user.php?id=testuser&var1=1&var2=2&var3=3
HOWEVER, this rewrite should not use user.php, but index.php, in case someone enters:
http://www.domain.com or http://domain.com
This is what I got so far, however it doesn't seem to work.
Any help from a mod rewrite expert would be much appreciated.
Thanks in advance.
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^(.*)/?(.*)/?(.*)/?$ index.php?var1=$1&var2=$2&var3=$3 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(www|mail).domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+).domain.com$ [NC]
RewriteRule ^(.*)/?(.*)/?(.*)/?$ user.php?id=%2&var1=$1&var2=$2&var3=$3 [NC,QSA,L]
Your rules are actually very close to doing what you want them to. The only problem that I can see is in your test patterns for your two RewriteRule statements. Currently, you have
RewriteRule ^(.*)/?(.*)/?(.*)/?$ ...
...which happens to be equivalent to this:
RewriteRule ^(.*)$
This is because everything past the first capture group can match nothing and still be considered a match, so that first group is greedy and matches the whole input string without needing to defer to the other parts of the pattern.
Since the capture groups shouldn't capture forward slashes anyway, as they're being used as a variable delimiter here, the straightforward fix is to change them to [^/]*, as so:
Edit: I also modified the RewriteCond set in the second group to ignore the !-f condition in the case of /index.php, which will happen if you request the subdomain without anything after the domain.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?$ index.php?var1=$1&var2=$2&var3=$3 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^/index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(www|mail).domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+).domain.com$ [NC]
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?$ user.php?id=%2&var1=$1&var2=$2&var3=$3 [NC,QSA,L]

Resources