I want to use a wildcard so any page addresses are allowed. But I want to use some static page addresses like user login, registration, and some more. I'm currently using this method but it will, as I expected, try to find page.php on every page address.
# KONTO
RewriteRule ^skapa-ett-konto$ account-create.php [L]
RewriteRule ^logga-ut$ account-logout.php [L]
# KONTO: Sidor som behöver laddas in genom jQuery/AJAX
RewriteRule ^slutfor-registreringen$ configurations/javascripts/ajax-form/account-create.php [L]
RewriteRule ^logga-in$ configurations/javascripts/ajax-form/account-login.php [L]
# --------------------------------------- #
# PROFIL
RewriteRule ^anvandare/([a-z0-9]+)$ profile.php?u=$1 [L]
# --------------------------------------- #
# ENSKILDA FILER
RewriteRule ^(.*)$ page.php?p=$1 [L]
How can I make so RewriteRule use wildcard to other page addresses than for the static ones?
Thanks in advance.
If I understood right, you want to exclude some existing pages like login and registration from the last rule. The way to exclude them is like this:
# ENSKILDA FILER
# Add other pages to exclude here
RewriteCond %{REQUEST_URI} !(page\.php|login|registration) [NC]
RewriteRule ^(.*)$ page.php?p=$1 [L]
If all other addresses are not files, you can prefix your page.php rule with
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* page.php?p=$0 [L]
Related
Here are the .htaccess rules:
RewriteEngine On
RewriteRule ^([^/]*)$ content.php?seourl=$1 [L]
RewriteRule ^pdf/([^/]*)$ content-single.php?seourl=$1 [L]
RewriteRule ^browsepdf$ browse.php [L]
RewriteRule ^browsepdf-([^/]*)$ browse.php?page=$1 [L]
RewriteRule ^download/([^/]*)$ download.php?pdf=$1 [L]
RewriteRule ^sitemap.xml$ xmlsitemap.php [L]
Options -Indexes
Here every URL is pointing to content.php?seourl=$1, even css, js and image files.
Here are some example URLs I need,
http://example.com/sjskjfsk21
http://example.com/asfasfasf43sf
http://example.com/pdf/fhfdhdh3432aaf
http://example.com/pdf/aisfyiahm2faf3
http://example.com/browsepdf
http://example.com/browsepdf-1
http://example.com/browsepdf-2
http://example.com/download/fjaskfjalsk3rs
http://example.com/download/usaydiy7aisydi
http://example.com/sitemap.xml
Can anyone please fix the .htaccess file.
Here every url is pointing to "content.php?seourl=$1"
Because your first (generic) rule catches all the requests. You need to change the order so you have the most specific rules first, and the most generic (catch-all) rules at the end. In your case you just need to move the first rule to the end. For example:
RewriteRule ^pdf/([^/]*)$ content-single.php?seourl=$1 [L]
RewriteRule ^browsepdf$ browse.php [L]
RewriteRule ^browsepdf-([^/]*)$ browse.php?page=$1 [L]
RewriteRule ^download/([^/]*)$ download.php?pdf=$1 [L]
RewriteRule ^sitemap\.xml$ xmlsitemap.php [L]
RewriteRule ^([^/]*)$ content.php?seourl=$1 [L]
NB: I backslash-escaped the dot in sitemap.xml to match a literal dot, otherwise it matches any character.
even css, js and image files.
You can make an exception for these static resources at the beginning of your file, before the existing directives. For example:
RewriteRule \.(js|css|png|jpg|gif)$ - [L]
For any URL that ends in any of the stated file extensions then stop processing the current mod_rewrite rules.
Alternatively (although perhaps marginally less efficient), you can prevent processing of requests for files that exist. Again, this goes before your existing mod_rewrite directives. For example:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
However, this must now check every request for the existence of a file on the filesystem that maps to the request. (It could also be combined with the above rule if required.)
UPDATE: Bringing this together, we have:
# Exclude any existing files from being rewritten
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Specific rewrites
RewriteRule ^pdf/([^/]*)$ content-single.php?seourl=$1 [L]
RewriteRule ^browsepdf$ browse.php [L]
RewriteRule ^browsepdf-([^/]*)$ browse.php?page=$1 [L]
RewriteRule ^download/([^/]*)$ download.php?pdf=$1 [L]
RewriteRule ^sitemap\.xml$ xmlsitemap.php [L]
# Any other requests for the form "/<anything>"
RewriteRule ^([^/]*)$ content.php?seourl=$1 [L]
Most of the RewriteRules are working fine but there are a couple that have the same word in them and aren't going to the correct page.
Here is my full htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
# Rewrites for main pages
RewriteRule home index.php
RewriteRule about-us about-us.php
RewriteRule business-advice business-advice.php
RewriteRule associates associates.php
RewriteRule become-an-associate associates-sign-up.php
RewriteRule blog blog.php
RewriteRule contact-us contact-us.php
RewriteRule log-in log-in.php
RewriteRule sign-up sign-up.php
The problem resides within the two associated links. When I go to [MYURL]/associates it works fine but if I go to [MYURL]/become-an-associate it takes me to the correct URL but shows content from [MYURL]/associates
Anybody got any ideas?
Thanks,
The pattern for RewriteRule's are regular expressions and the rules all loop until the URI stops changing. That means the first time around, when you request /become-an-associate, it matches and is rewritten to /associates-sign-up.php. Then, the second time around, the rule with the pattern associates matches because of "/associates-sign-up.php". You need to add boundary checks (e.g. ^ and $) as well as the [L] flag:
# Rewrites for main pages
RewriteRule ^home/?$ index.php [L]
RewriteRule ^about-us/?$ about-us.php [L]
RewriteRule ^business-advice/?$ business-advice.php [L]
RewriteRule ^associates/?$ associates.php [L]
RewriteRule ^become-an-associate/?$ associates-sign-up.php [L]
RewriteRule ^blog/?$ blog.php [L]
RewriteRule ^contact-us/?$ contact-us.php [L]
RewriteRule ^log-in/?$ log-in.php [L]
RewriteRule ^sign-up/?$ sign-up.php [L]
A bit of help fellow SO people.
What I have at the moment (based on some code I used for a different type of URL).
I want the first URL to redirect to the second, with no query string included afterwards
This is what I have to so far.
RewriteRule ^(page.php?id=missionstatement+)/?$ http://example.com/why/mission-statement [R=301,L]
RewriteRule ^(page.php?id=ofsted+)/?$ http://example.com/how/ofsted-report [R=301,L]
RewriteRule ^(page.php?id=governingbody+)/?$ http://example.com/governors [R=301,L]
Here is the rule (will redirect 1 URL):
RewriteCond %{QUERY_STRING} ^id=whatever
RewriteRule ^page\.php$ http://%{HTTP_HOST}/how/somehow? [R=301,L]
This rule intended to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
I have used %{HTTP_HOST} -- this will redirect to the same domain as requested URL. If domain name has to be different, replace it by exact domain name.
The ? at the end of new URL will get rid of existing query string.
Ahoy!
Give this a whirl:
#check mod_rewrite is enabled
<IfModule mod_rewrite.c>
#enable mod rewrite
RewriteEngine On
#set working directory
RewriteBase /
#force trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [R=301,L]
#bootstrap index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page.php\?id=(.*)$ http://www.willans.com/page.php/$1 [R=310,L]
#end mod rewrite check
</IfModule>
It's been a while since i've done any web dev, but that should be a push in the right direction at least ;)
My mod_rewriting seems to be being messed up by this one line at the end of my .htaccess file, and I can't for the life of me work out why.
RewriteEngine ON
Options FollowSymLinks
# User profile with username specified
RewriteRule ^([a-z0-9_.-]{1,30}+)$ profile.php?username=$1 [NC,L]
I want to match usernames, but allow them to have a-z 0-9 (no case) and also allow underscores, dots and hyphens.
It works fine without the '_.-'
I've tried escaping them too, but to no avail.
EDIT:
It seems that the problem with the rewrite, is that it is causing my 'styles.css' file to be rewritten, even though I've got it set to NOT rewrite, if the file or directory exists.
Here's the whole .htaccess file...
RewriteEngine ON
Options FollowSymLinks
# Only rewrite for folders and directories that don't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Remove www.
RewriteCond %{HTTP_HOST} ^www.nitpicker.me$ [NC]
RewriteRule ^(.*)$ http://nitpicker.me/$1 [R=301]
# Remove the trailing slash if there is one
RewriteRule ^(.+)/$ $1
# Main pages
RewriteRule ^(stream|settings|comment|profile)(.php)?$ $1.php [QSA,L]
# Find friends page
RewriteRule ^friends$ findfriends.php [L]
RewriteRule ^subject-([0-9]+)$ page.php?subject_id=$1 [QSA,L]
RewriteRule ^nit-([0-9]+)$ comment.php?nit_id=$1
RewriteRule ^search-([a-z0-9]+)$ search.php?term=$1 [NC,L]
# The initial sign up page with invite code
RewriteRule ^signup(-([a-z0-9]+))?$ signup.php?invite=$2 [NC,L]
# Trending page
RewriteRule ^(newest|trending|most_picked) trending.php?select=$1 [QSA,L]
# User profile with username specified
RewriteRule ^([a-z0-9\-_\.]{1,30}+)$ profile.php?username=$1 [NC,L]
How can I get it to stop it rewriting my '/styles.css' file?
use this:
# User profile with username specified
RewriteCond %{REQUEST_URI} !^.*\.css.*$ [NC]
RewriteRule ^([a-z0-9\-_\.]{1,30}+)$ profile.php?username=$1 [NC,L]
Here is what I'm trying to accomplish:
(this .htaccess file is located in mysite.com/test/):
http://mysite.com/test/admin go to http://mysite.com/test/admin/index.php
http://mysite.com/test/contact go to http://mysite.com/test/contact.php
http://mysite.com/test/salt-lake-city/ go to http://mysite.com/test/index.php/city=salt-lake-city
http://mysite.com/test/salt-lake-city/deals/ go to http://mysite.com/test/deals.php?city=salt-lake-city
To start, I have:
RewriteRule ^(.*)/(.*)\.php$ $2.php?city=$1 [L]
(this handles the last 2). But, when I try to add the admin clause:
RewriteRule ^admin/ admin/index.php [L]
RewriteRule ^(.*)/(.*)\.php$ $2.php?city=$1 [L]
It messes up (the css is out of whack) etc.
Any thoughts?
Well .. if I had to do these rewrite rules, I would do them like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# admin rewrite
RewriteRule ^admin$ admin/index.php [L]
# rewrite /contact --> /contact.php (and similar)
# add .php extension if such file does exist
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]
# OR
# alternatively specify their names directly
# plus it is more precise for the example you have provided
# (that's if you need to rewrite only specific pages)
RewriteRule ^(contact|about|something)$ $1.php [L]
# /salt-lake-city/ --> /index.php?city=salt-lake-city
RewriteRule ^([^/]+)/$ index.php?city=$1 [QSA,L]
# /salt-lake-city/deals/ --> /deals.php?city=salt-lake-city
RewriteRule ^([^/]+)/deals/$ deals.php?city=$1 [QSA,L]
To start, I have:
RewriteRule ^(.*)/(.*)\.php$ $2.php?city=$1 [L]
(this handles the last 2).
Sorry, but I do not see how "it will handle the last 2". I see no .php in the last two URL examples you have provided.
It messes up (the css is out of whack) etc.
Well -- let's see how it will work with my rules. In any case -- it may also depends how you wrote links to css/images/js.