I've been tring for some time now with htaccess to rewrite this url
http://www.domain.com/folder/file.php?id=whatever
to
http://www.domain.com/?id=whatever
Any help would be much appreciated !
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^$ folder/file.php?id=$1 [QSA,L]
Must be something like
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ folder/file.php [QSA]
look for the exact syntax urself ... also notice the QSA flag ... actually it redirects the query string from called url to actual url ... also you can not match the part after '?' ie the query string in the regex ...
Related
I was getting problem in htaccess. I have tried a lot examples but yet not able to write perfect Rewrite Rules as I want. Please help me.
http://domainname.com/sub-category.php?mc=eatry
to
http://domainname.com/eatry
AND
http://domainname.com/explorelisting.php?mc=eatry&&sc=restaurant
to
http://domainname.com/eatry/restaurant
.htaccess (I have tried)
RewriteEngine On
RewriteRule ^/(.*) ./sub-category.php?mc=$1
RewriteRule ^/(.*) ./explorelisting.php?mc=$1&&sc=$1
RewriteRule ^([^\.]+)$ $1.php [NC,L]
A few things to note.
In the URL you don't need && only & when specifying another parameter.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\w+) sub-category.php?mc=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\w+)/(\w+) explorelisting.php?mc=$1&sc=$2 [QSA,NC,L]
The regular expression assumes that the query will only accept any letter - capital or lowercase.
RewriteCond is used to check that the URL is not a directory and not a file, if it isn't, then it rewrites to the rule.
This is used because the URL http://www.example.com/index would get redirected to http://www.example.com/sub-category.php?mc=index which is undesired.
It might be prudent to restructure your rule. Perhaps having the URLs looking like http://www.example.com/category/eatry and then modifying the first rule to being RewriteRule ^category/(\w+) sub-category.php?mc=$1 in order to prevent ambiguity.
The flags (what is at the end of each rule in square brackets) are as follows:
QSA - Query String Append:
This enables you to have the parameters at the end of the string (anything following the ?).
NC - No Case:
This means that your rule is case insensitive.
L - Last:
This will terminate the htaccess if the rule is matched.
I want to create .htaccess mod rewrite but still have problem.
Suppose i created friendly URL like this :
mydomain.com/mypage/12345/title-of-news
I want that friendly URL process the below URL in hidden process :
mydomain.com/index.php?p=mypage&idnews=12345
Values of "p" and "idnews" are dynamic, so they will have different value.
I tried below code but still didn't work. Anything wrong?
RewriteBase /
RewriteCond %{QUERY_STRING} p=(\w+)&idnews=(\d+)
RewriteRule ^index.php /%1/%2 [L,R=301]
Any help would be appreciated. Sorry if this is duplicated question, if don't mind please tell me the answer link. Thanks a lot.
I think you don't need the RewriteCond you write there.
And you can use following rule
RewriteRule ^/?([0-9a-zA-Z_.-]+)/([0-9]+)/([0-9a-zA-Z_.-]+)$ index.php?p=$1&idnews=$2 [L]
If you want to change index.php?p=foo&idnews=bar to /foo/bar , try theses rules :
RewriteEngine on
##externally redirect "/index.php?p=foo&idnews=bar" to "/foo/bar"##
RewriteCond %{THE_REQUEST} /index\.php\?p=([^&]+)&idnews=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [L,R,NC]
##internally redirect "/foo/bar" to "/index.php?p=foo&idnews=bar"##
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?(.*)?$ /index.php?p=$1&idnews=$2 [NC,L]
If user access this URL(http://domain.com/products/view-product.php?productID=21) in browser, it should show the page http://domain.com/new/view-product.php?pID=21
I have tried bellow code but it wont working, so please help me to resolve.
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} products/view-product.php
RewriteRule ^products/view-product.php?productID=21$ new/view-product.php?pID=21 [R=301,L]
products/view-product.php are physically exists in the server, still i want to rewrite it to the new page that i have designed now.
You can't match against the query string (everything after the ?) in a rewrite rule. Only the URI is used to match against that regular expression. You need to use the condition:
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} products/view-product.php\?productID=21($|\ |&)
RewriteRule ^products/view-product.php$ /new/view-product.php?pID=21 [R=301,L]
You can match query string using RewriteCond and %{QUERY_STRING} variable. Query string params didn't get to RewriteRule.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^productID=21$
RewriteRule ^products/view-product\.php$ /new/view-product.php?pID=21 [R=301,L]
Or if you want to redirect all productID's:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^productID=([0-9]+)$
RewriteRule ^products/view-product\.php$ /new/view-product.php?pID=%1 [R=301,L]
GET the value of productID and then redirect to the new URL.
please help, how do I retrieve data "page" from the following url:
mysite/start/search/indonesia/bali/18Jul2014/19Jul2014/100?page=2
if i use htaccess to direct?
This part of the line my htaccess
RewriteRule start/search/(.*)/(.*)/(.*)/(.*)/(.*)?pg=(.*)$ printdata.php?data=$1+$2+$3+$4+$5+$6 [QSA,L]
please help .. thank you response correction.
You need to match against the %{QUERY_STRING} or %{THE_REQUEST} variables. The query string (everything after the ?) isn't part of the URI that you match against in rewrite rules.
So:
RewriteCond %{THE_REQUEST} \ /+start/search/(.*)/(.*)/(.*)/(.*)/(.*)\?pg=(.*)$
RewriteRule ^ /printdata.php?data=%1+%2+%3+%4+%5+%6 [QSA,L]
Or
RewriteCond %{QUERY_STRING} ^pg=([0-9]+)
RewriteRule ^start/search/(.*)/(.*)/(.*)/(.*)/(.*)$ /printdata.php?data=$1+$2+$3+$4+$5+%1 [QSA,L]
htaccess is enabled, i have the canonicalization running (no-www to www.)
I'm trying to use htaccess to do the following
www.domain.com/page.php?i=Page1
www.domain.com/page.php?i=Page2
To
www.domain.com/Page1
www.domain.com/Page2
I tried using this code snippet, with no luck so far:
rewriterule ^([a-zA-Z0-9_-]+)/$ page.php?i=$1
However I think I'm going the reverse way. I can't find example for this.
I have this but I can't make it work.
Try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/page\.php$
RewriteCond %{QUERY_STRING} ^i=([a-zA-Z0-9_-]+)$
RewriteRule ^(.*)$ http://www.domain.com/%1? [R=302,L]
taken from: http://www.simonecarletti.com/blog/2009/01/apache-query-string-redirects/
You want www.domain.com/Page1 to be visible in the browser's address bar, but internally that url should actually /page.php?i=Page1. In that case the problem is the trailing / in your regex:
Rewriterule ^([a-zA-Z0-9_-]+)/$ page.php?i=$1
^---here
Your desired /Page1 url has no trailing slash, yet your rewrite regex requires one, so the pattern doesn't match, and no rewriting occurs. Try removing the / and see if that helps any:
Rewriterule ^([a-zA-Z0-9_-]+)$ page.php?i=$1
^---no /
In order to parse out the query string you need to access it in a RewriteCond
# We need this line because /page.php?i=page.php will cause an infinite loop
RewriteCond %{QUERY_STRING} !i=page.php
# Now we parse out the value of i from the query string
RewriteCond %{QUERY_STRING} i=([^&]+)
RewriteRule ^page.php /%1 [L]