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
Related
I'm using the following configuration in .htaccess to try to rewrite URLs of the form http://www.mysite.com/subdir/page.php?param=4 to http://www.mysite.com/subdir/page/param/4:
# Turn on the rewrite engine
Options +FollowSymlinks
RewriteEngine on
# Request routing
RewriteRule ^([a-zA-Z_-]*)$ index.php?name=$1 [nc,qsa]
However, I get an Internal Server Error when I visit the page. Is there something wrong with the configuration?
EDIT: I've updated it to the following, but now it does not seem to be doing any rewriting and gives me a 404 when I try to use a URL like http://www.mysite.com/subdir/param/2:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^name=([0-9]+)$ [NC]
RewriteRule ^project-testing/ws/index.php$ /project-testing/ws/name/%1 [R=301,NC,L]
You need to use %{QUERY_STRING} to read the query string of your URL to redirect based on it.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^param=([0-9]+)$ [NC]
RewriteRule ^subdir/page.php$ subdir/page/param/%1 [R=301,NC,L]
Or if you want that accessing:
http://www.mysite.com/subdir/page/param/4
Shows the content of:
http://www.mysite.com/subdir/page.php?param=4
Then it would be like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^subdir/page/([^/]+)/([^/]+)$ subdir/page.php?$1=$2 [NC,L]
Can you any one see anything wrong with the following apache rewrite rule:
This is in my .htaccess file inside a folder called "text" a subdirectory of localhost/lombardpress I have the following rule
Options +FollowSymlinks
RewriteEngine on
RewriteRule ([^/]+) /textdisplay.php?fs=$1 [NC]
I was expecting this input:
http://localhost/lombardpress-dev/text/lectio1
to rewrite to this:
http://localhost/lombardpress-dev/text/textdisplay?fs=lectio1
But instead I get a 404 error.
The requested URL /textdisplay.php was not found on this server.
It looks to me like the RewriteRule has re-written the address but not as I intended - so there must be something wrong with my regular expression.
Let me know if I can provide further information.
Try this
Options +FollowSymlinks
RewriteEngine on
RewriteBase /lombardpress-dev/text/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/]+) textdisplay.php?fs=$1 [NC]
With that rewrite cond you wont redirect textdisplay.php to itself again.
The problem is that [^/]+ matches all but / so it matches even textdisplay.php
Remove leading slash in target URL:
Try this code:
Options +FollowSymlinks
RewriteEngine on
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ textdisplay.php?fs=$1 [NC,L,QSA]
Reference: Apache mod_rewrite Introduction
Get rid of the / in front of the target:
RewriteRule ([^/.]+) textdisplay.php?fs=$1 [NC]
# no slash here ----^
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]
I would like to redirect mysite.com/index.php to mysite.com/index.php?id_category=12&controller=category
Here is my htaccess file.
But it doesn't work. Chrome says : This webpage has a redirect loop.
Options +FollowSymLinks +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
RewriteRule index\.php http://mysite.com/index.php?id_category=12&controller=category
This should work:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteCond %{QUERY_STRING} ^$
RewriteRule (.*) http://www.mywebsite.com/index.php?foo=bar [R=301,L]
The first condition checks if the URI is equal to index.php and the second one checks if GET values are empty. The AND between the 2 conditions is implicit here.
Probably you should change use another file name instead of index.php for the destiny. But you can try:
RewriteRule index.php index.php?id_category=12&controller=category
I have created a site in a subdirectory and would like the site to appear as if it's in the root.
I used the below Mod_Rewrite code to get the site root to redirect to the subdirectory, but I would like the folder the files are held in to not appear.
currently: www.example.com/sitefiles/content/
Would like: www.example.com/content
Thanks
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^$ /sitefiles/ [L]
RewriteRule (.*) /sitefiles/$1 [L]
If it needs to be done via .htaccess and mod_rewrite, then use this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/sitefiles/
RewriteRule (.*) /sitefiles/$1 [L]
Since you do not explicitly specify the page when website root will be hit, then there is no need for this line: RewriteRule ^$ /sitefiles/ [L]
You need to add condition for your main rewrite rule to prevent rewrite loop (when already rewritten URL gets rewritten again): RewriteCond %{REQUEST_URI} !^/sitefiles/
Well, the directory should have no reason to appear in any listing, and that mod_rewrite you posted should allow you to do as you said. Still, I would go about it a different way:
Options +FollowSymLinks +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^$ sitefiles/ [L]
RewriteRule ^(.+)$ sitefiles/$1 [L]
Hope this works.