I have the folowing rewrite rule
<filesMatch "^(member)$">
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/(.*)/(.*)/(.*)$ /member-profile.php?ID=$2 [L]
</filesMatch>
<filesMatch "^(community-events)$">
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/(.*)/(.*)/(.*)/(.*)$ /community-events.php?ID=$3 [L]
</filesMatch>
Which is, obviously rewriting this
mydomain.com/comunity-events/category/id/name
to this
mydomain.com/comunity-events.php?myvariables
Now, I want a new redirect which doesn't have a starting "folder", like this
mydomain.com/business-category/business-name
to
mydomain.com/business-profile.php?variables
Is that even possible, given the current configuration, without making a redirect for each category name?
You don't even need filesMatch directives as you can match starting part in RewriteRule itself.
You can replace your .htaccess with these rules:
Options +FollowSymlinks -MultiViews
RewriteEngine On
# handle /member/...
RewriteRule ^/?(member)/(.*)/(.*)$ /member-profile.php?ID=$2 [L,QSA,NC]
# handle /community-events/...
RewriteRule ^/?(community-events)/(.*)/(.*)/(.*)$ /community-events.php?ID=$3 [L,QSA,NC]
# handle /business-category/business-name
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)/(.*)$ business-profile.php?name=$2 [L,QSA]
Related
This is my htaccess code for url-rewriting.
I have a problem here.
Options +FollowSymLinks -MultiViews -Indexes
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
ErrorDocument 404 404error.php
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)?$ specification.php?url=$1
RewriteRule ^([A-Za-z0-9-]+)?$ news.php?url=$1
RewriteRule ^([A-Za-z0-9-]+)?$ social.php?url=$1
</IfModule>
here are three pages with different data. When I click for spcification.php page data then it will redirect me on same page but if my this code is above from this then it will redirect me to on news.php.
RewriteRule ^([A-Za-z0-9-]+)?$ news.php?url=$1
only top condition work not others.
From what I know you cannot use same rule for multiple conditions like this
RewriteRule ^([A-Za-z0-9-]+)?$ specification.php?url=$1
When first rule matched it stop searching for same condition, instead you have to use
RewriteRule ^specification/([A-Za-z0-9-]+)?$ specification.php?url=$1 [L]
RewriteRule ^news/([A-Za-z0-9-]+)?$ news.php?url=$1 [L]
Your original url will look like this domain.com/news/value.
Updating a site and have a ton of redirects.
The issue is that the new URL retains part of the old for all, as seen in the following example:
Redirect 301 /old/oldfile.html /new
redirects to:
http://url.com/new/oldfile.html
Aside from the redirects the htaccess contains the following:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
Options +FollowSymLinks
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Use mod_rewrite instead. This should go above the other rewrite rules.
RewriteEngine on
RewriteRule ^old/oldfile.html /new [R=301,L]
Here's my .htaccess file
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I need this www.domainname.com/folder-name/
to rewrite/redirect to this
www.domainname.com/folder-name/index.html
without screwing up what's already in htaccess.
So i'm clear...when it's all said and done i want www.domainname.com/folder-name/ to display in the URL bar, but I want the content from index.html to be rendered.
You have a problem in that if you go to www.domainname.com/folder-name/, your first rewrite rule will redirect you to www.domainname.com/folder-name, removing the trailing slash. Assuming you haven't turned off DirectorySlash, mod_dir will redirect this back to www.domainname.com/folder-name/, which then triggers the rewrite rule, which triggers mod_dir, etc. If you've turned off DirectorySlash, that means you've exposed an information disclosure bug (or "feature") which will list the contents of the folder-name folder even if index.html exists.
So if you really want to be able to go to www.domainname.com/folder-name/ and not lose the trailing slash, then you need to add a condition to your first rule and then simply allow mod_dir to do its thing:
DirectoryIndex index.html
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If you've mistyped and you actually want all the trailing slashes to get removed including for folders, leaking the folder contents and all, then you've got to do a bunch of other things. Essentially, you have to do what mod_dir normall does for you anyways and make sure DirectorySlash is turned off (still dangerous, as it could expose the file listings of all your folders):
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{DOCUMENT_ROOT}/$1/index.html -f
RewriteRule ^(.*[^/])$ /$1/index.html [L]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Please can somebody take a look at my .htaccess code to help identify what's wrong.
I am trying to amend all example.php, example.html and example.htm URL's to index.php?filename=example.html
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(index\.php) - [L]
RewriteRule ^([^/\.]+).(php|html|htm)$ index.php?filename=$1.html&%{QUERY_STRING} [L]
</IfModule>
When I enter a URL like example.html I'm taken to the default 404 and not index.php
Try:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?filename=$1 [L,QSA]
</IfModule>
I'm not using the apache .hatacces for a while,
But try this one:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} !^.*/(css|img|js)($|/.*$) [NC]
RewriteRule ^(.*)$ /index.php?filename=$1&%{QUERY_STRING} [L]
Basically what you need is to be sure that the URL didn't have already index.php. You accomplish it with RewriteCond.
After you redirect all to index.php passing the filename as a parameter.
You can add more exception to the RewriteCond like on this example, all content with /css, /js, /img will not pass to this rule.
You can check this thread: Using .htaccess to reroute all requests through index.php EXCEPT a certain set of requests also
I have mod rewrite enabled to remove all page extentions....
This is done in httpd.conf as I am using apache on windows
the setup I have is
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>
and this makes domain.com/bookings.html
appear as domain.com/bookings
I have php enabled in html files, so it parses all pages for php
But I have now put a search box on my site, and if I search for "music" it will use the url:
domain.com/search?q=music
I would like my urls to look like:
domain.com/search/music
But also, I would like to be able to type
domain.com/search/abba
And it would load the page "search.html" lets call it "search" and it would add the parameter ?q=abba , but then still look like the above example in the URL bar
I'm sure this can be achieved with mod rewrite but I am not sure how to phrase the expression.
Thanks in advance for any help I receive :)
use the kind of URLs in your html: domain.com/search/music
add this in your .htaccess file in your DocumentRoot.
Options +FollowSymLinks +Indexes -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} (search)/([\w\d]+) [NC]
RewriteRule ^ %1.html?q=%2 [L,QSA]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
or add this to your .conf file
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{REQUEST_URI} (search)/([\w\d]+) [NC]
RewriteRule ^ %1.html?q=%2 [L,QSA]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>
You may just add one simple RewriteRule:
RewriteRule ^/search/([a-zA-Z0-9]+)$ /search?q=$1 [QSA,NC,L]
So all in all:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^/search/([a-zA-Z0-9]+)$ /search?q=$1 [QSA,NC,L]
</IfModule>
Oh by the way:
Here's the wiki of serverfault.com
The howto's htaccess official guide
The official mod_rewrite guide
And if that's not enough:
Two hints:
If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess files), try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)