My attempt:
RewriteCond %{QUERY_STRING} ^id=(.*)$ [NC]
RewriteRule ^/product$ /product/%1 [NC,L,R=301]
I want to apply this rule to /product/ and /supplier/ directories only. They are both first level sub directories.
Note: product/?id={xxx} is actually product/index.php?id={xxx}. Apache hides my extensions and indexes. Just want to point that out.
My product/index.php handles the parameter given and determines what page it should show:
index.php
if ( isset( $_GET['id'] ) && !empty( $_GET['id'] ) ) {
//html for individual page e.g. /product/?id=foo
//e.g. <h1><?= $_GET['id'] ?> Page</h1>
} else {
//html for product list e.g. /product/ (no parameters)
}
Try this in one .htaccess file at root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} id=(.+) [NC]
RewriteRule ^(product|supplier)/?$ /$1/%1? [NC,L,R=301]
In .htaccess files the URI-path test it in the rule does not have a leading slash (^/product), therefore the regex cannot have it either. The trailng ? deletes the incoming query.
If the rule-set is to be placed at Apache main configuration files, the leading slash should be kept: ^/(product|supplier)/?$
UPDATE
To show the wanted URL but still get the data from the original URL.
Request: /product/?id=parameter
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s/(product|supplier)/\?id=([^\s]+) [NC]
# Strip the query and redirect permanently
RewriteRule ^(product|supplier) /$1/%3? [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^$
# Map internally to the original request
RewriteRule ^(product|supplier)/([^/]+)/? /$1/?id=$2 [L,NC]
Another option is to use directly the "pretty" URL in the request:
Request /product/parameter to /product/?id=parameter
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(product|supplier)/([^/]+)/? /$1/?id=$2 [L,NC]
Related
I have a page that is www.example.com/page.html with a link on it that goes to on a blank page www.example.com/folder/link.php?id=123456
For a specific reason, when I'm on the same page but in other language (page will be like www.example.com/page_ro.html) I cannot change this link and need to use rewrite condition to do change that same link.
Let say I need the link to become www.example.com/folder/link.php?id=78910 in this case.
How can I do that in this case ? I've tried this with no success :
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} ^http://www\.example\.com/page_ro.html [NC]
RewriteCond %(REQUEST_URI) folder/link\.php?id=123456$ [NC]
RewriteRule ^(.*) http://www.example.com/link.php?id=78910 [L]
You cannot match query string using REQUEST_URI variable. Use QUERY_STRING instead like this inside /folder/.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder/
RewriteCond %{HTTP_REFERER} ^http://www\.example\.com/page_ro.html [NC]
RewriteCond %(QUERY_STRING) ^id=123456 [NC]
RewriteRule ^(link\.php)$ $1?id=78910 [NC,L,R=302]
I want to change my url
http://www.abc.com/search_result.php?id=110
to
http://www.abc.com/110
Here is the code which i am using.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search_result\.php\?id=([^\s]+) [NC]
RewriteRule ^ http://abc.com/%1? [R=301,L]
But the problem is, url changed to http://www.abc.com/110 , but page remain same.
Please anybody help !
One thing more i want to ask . Suppose i want to add more parameter in original url:
Say,
http://www.abc.com/search_result.php?id=110&name=amit
then what i should do to get the result.
http://www.abc.com/i-am-amit
Thanks !
You need an internal rewrite rule also for showing actual content from search_result.php"
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search_result\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ http://abc.com/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ search_result.php?id=$1 [NC,L,QSA]
Also for http://www.abc.com/search_result.php?id=110&name=amit how do you want pretty URL to be? Keep in mind that you will need both id & name in pretty URL such as:
http://www.abc.com/110/amit
Is that how you want?
My goal is to achieve the following:
My query: properties.php?id=1234-N-StreetName-etc
to be turned into:
properties/1234-N-Beverly-Blvd.html
properties.php will evaluate the value of $id (In this case any-address.html) against a mysql database. If a value is not in the database, the file properties.php will redirect the user to a 404 page.
Where $id's value can be any combination of alphabetical characters, dash '-', or numbers.
The code I have is not working:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]
RewriteRule ^properties/([a-zA-Z0-9_-]+)\.html$ properties.php?id=$1
Can you please help me get the right htaccess rewrite-code?
PS. I know nothing about .htaccess. I modified this code form a sample website.
You will need L flag to mark that rule as Last and QSA for Query String Append.
Use this code in .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^properties/([^.]+)\.html$ /properties.php?id=$1 [L,NC,QSA]
To apply this rule in a sub directory sun use this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /sub
RewriteRule ^properties/([^.]+)\.html$ /sub/properties.php?id=$1 [L,NC,QSA]
# translate html -> php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)\.html$ /$1.php [L,NC]
This will handle URI such as /sub/properties/abc-123.html and forward it to /sub/properties.php?id=abc-123
I have a directory structure like this
/ub/
/ub/img/
/ub/inc/
Basically I want to change my img URL to it's parent directory (ub),
When i access http://localhost/ub/mypic.jpg
First, it has to check /ub/img/mypic.jpg, if exist then get that file and pass to the browser, if not then just pass it to http://localhost/ub/index.php?img=mypic.jpg with INTERNAL REDIRECTION.
Second, index.php will create mypic.jpg and store it to /ub/img/, then pass mypic.jpg to the browser.
So, whether the file is exist or not, the browser will only have http://localhost/ub/mypic.jpg as the URL
So far, i have this on my .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/ub/img/$1 -f
RewriteRule .+ /ub/img/$1 [L]
I have 2 days to find out the solution, but, it doesn't seem to work,
Thanks,
EDIT :
Hey, I've got this, The request to http://localhost/ub/app/file.php is already passed to index.php?uri=app/file.php as i expected,
but the request to http://localhost/ub/img/mypic.jpg is not passed to index.php?uri=img/mypic.jpg. Is there any solution?
Options -MultiViews +FollowSymLinks
RewriteEngine On
RewriteBase /ub/
RewriteCond %{REQUEST_URI} ([^/]+)$
RewriteCond %{DOCUMENT_ROOT}ub/img/%1 -f
RewriteRule ^(.+)$ img/%1 [L]
RewriteCond %{REQUEST_URI} ([^/]+)$
RewriteCond %{DOCUMENT_ROOT}ub/inc/%1 -f
RewriteRule ^(.+)$ inc/%1 [L]
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
Try something like this (place it in .htaccess in the /ub folder):
Options +FollowSymLinks
RewriteEngine on
RewriteBase /ub/
RewriteCond %{REQUEST_URI} ([^/]+\.jpg)$
RewriteCond %{DOCUMENT_ROOT}/ub/img/%1 -f
RewriteRule .+ - [L]
RewriteRule ^(.*)$ index.php?img=$1
How would you redirect (301?)?:
test.com/?dep=CAR (the variable is dynamic, whatever comes after the=)
to a subdirectory:
test.com/CAR/
This is what I've got so far:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^dep=(.*)
RewriteRule ^.* / [L,R=301]
I test this on a localhost (WAMP).
And the main directory is localhost/newsite/
(localhost/newsite/?dep=CAR)
, but I guess that shouldn't matter? Online or localhost.
Try the following
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
#if it has a dep param, capture the value
RewriteCond %{QUERY_STRING} (^|&)dep=([^&]+) [NC]
#and redirect to /value/
RewriteRule .* /%2/? [L,R=301]
#if there is no dep param
RewriteCond %{QUERY_STRING} !dep= [NC]
#and capture the directory and pass as a dep param
RewriteRule ^([a-zA-Z]+)/$ ?dep=$1 [L]