Mod_rewrite - Multiple rules in .htaccess - .htaccess

I'm setting up a slightly more complexed web site than I normally are used to using a bespoke CMS system and have hit a stumbling block.
Basically what I am wanting to achieve is a simple top level page structure so the urls are clean as follows:
http://www.mywebsite.com/page.php?page_url=val1
http://www.mywebsite.com/val1
Not normally a problem, but the existing .htaccess already has several RewriteRules existing and any variation of adding a RewriteRule conflicts and prevents select pages from working.
Tried variations around...
RewriteCond %{QUERY_STRING} ^page=(.+)
RewriteRule ^$ /%1? [R=301,L]
With no joy.
Do I need to rethink my strategy or is there something I'm just quite simply overlooking?
Should I be rethinking towards making the top-level-category page 'market.php' do the work rewriting in the # Specify MARKET LEVEL rewrite
My existing .htaccess is as follows and works fine after a thorough testing but changing to include the new top level page causes it to error.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Force search engines to use www
RewriteCond %{HTTP_HOST} !^www\.mywebsite\.com$
RewriteRule ^(.*) http://www.mywebsite.com/$1 [R=301,L]
# Specify MARKET LEVEL rewrite
RewriteRule ^top-level-category-1$ /market.php?p=top-level-category-1 [L]
RewriteRule ^top-level-category-2$ /market.php?p=top-level-category-2 [L]
RewriteRule ^top-level-category-3$ /market.php?p=top-level-category-3 [L]
RewriteRule ^top-level-category-4$ /market.php?p=top-level-category-4 [L]
RewriteRule ^top-level-category-5$ /market.php?p=top-level-category-5 [L]
RewriteRule ^top-level-category-6$ /market.php?p=top-level-category-6 [L]
Options +FollowSymLinks
# Specify OFFER LEVEL rewrite
RewriteRule offer/(.*)/ offer.php?p=$1
RewriteRule offer/(.*) offer.php?p=$1
# Specify CLAIM OFFER LEVEL rewrite
RewriteRule get-offer/(.*)/ reveal.php?claim=$1
RewriteRule get-offer/(.*) reveal.php?claim=$1
# Specify CLAIM EVENT LEVEL rewrite
RewriteRule event-bonus/(.*)/ reveal-event.php?claim=$1
RewriteRule event-bonus/(.*) reveal-event.php?claim=$1
# Specify SEARCH LEVEL rewrite
RewriteRule search/(.*)/ search.php?p=$1
RewriteRule search/(.*) search.php?p=$1
# Specify EVENT LEVEL rewrite
RewriteRule offers/(.*)/ offers.php?p=$1
RewriteRule offers/(.*) offers.php?p=$1
# Specify BLOG LEVEL rewrite
RewriteRule blog/(.*)/ blog.php?p=$1
RewriteRule blog/(.*) blog.php?p=$1
RewriteRule view-blog/(.*)/ view-blog.php?p=$1
RewriteRule view-blog/(.*) view-blog.php?p=$1
# Specify EXPIRED OFFER LEVEL rewrite
RewriteRule offer-expired/(.*)/ offer-expired.php?p=$1
RewriteRule offer-expired/(.*) offer-expired.php?p=$1

Try using this RewriteRule in your .htaccess
RewriteEngine On
RewriteRule ^([^/]*)$ /page.php?page_url=$1 [L]
It should leave you with your desired URL.

Related

Implementing "friendly" URLs using .htaccess

I tried some of the other answers I could find in here, but it didn't work out. It's really simple though.
I want
/page?id=PAGENAME
to be accessible AND redirected to
/PAGENAME
Can you help me?
EDIT:
It feels like my already messed-up .htaccess file needs to be included in here. I already have basic rewriting enabled, but this feature is needed for two other "special pages". In the requested solution above, I would therefore just replace "page" with the two pagenames (it's danish names, so I thought it was easier this way).
Currently I have this. If you have any improvements to it, it's appreciated - but I just want this to work with the requested solution aswell.
# Options -Multiviews -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
# Always on https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# remove trailing slash
#RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
#301 Redirect everything .php to non php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+php?\ HTTP
RewriteRule (.+)\.php?$ http://MYURL.dk/$1 [R=301,L]
#Hide the .php from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#301 Redirect everything mistype after file extension -
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
#301 Redirect everything to current url -
RedirectMatch permanent /(.*).php/.* http://MYURL.dk/$1.php
RewriteCond %{REQUEST_FILENAME} -D
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L]
#301 Redirect from non www to www
RewriteCond %{HTTP_HOST} ^www.MYURL.dk [NC]
RewriteRule (.*) http://MYURL.dk/$1 [R=301,L]
#301 redirect index.php to /
RewriteBase /
RewriteCond %{REQUEST_URI} index.php
RewriteRule .* http://MYURL.dk/ [R=301,L]
#Deny access to songs
RewriteCond $1 !(loadmedia)\.php
RewriteRule ^songs/(.*)$ - [L,F]
Generally the URL in address bar should be like
www.siteurl.com/pagename/ for seo purpose and then read this url from .htaccess using rule which gives this query string parameter values in your php file.
.htaccess rule can be like
RewriteRule ^(.*)/$ /page?id=$1 [QSA,L]
It looks like you are wanting to implement "friendly" (or "pretty") URLs, making the URLs more friendly for you users (search engines don't really mind what your URLs look like).
The first step is to change all your on-page links to use the new "friendly" URL. So, you links should all be of the form /pagename (not /page?id=PAGENAME).
Then, in .htaccess, you need to internally rewrite this "friendly" URL into the real URL that your server understands. This can be done using mod_rewrite. In the .htaccess file in your document root:
# Enable the rewrite engine
Options +FollowSymLinks
RewriteEngine On
# Rewrite the "friendly" URL back to the real URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^id=
RewriteRule ^([\w-]*) /page?id=$1 [L]
If the file does not exist (!-f) and does not contain the id URL param then internally rewrite the request from /<pagename> to /page?id=<pagename>. This assumes your <pagename> consists only of the characters a-z, A-Z, 0-9, _ and -.
If this is a new site and the old URLs are not already indexed or referenced by external sites then you can stop here.
However, if you are changing an existing URL structure then you also need to externally redirect the real (ugly) URL to the "friendly" URL before the above internal rewrite. (This is actually what you are asking in your question.) In order to prevent a rewrite loop we can check against %{THE_REQUEST} (which does not change when the URL is rewritten).
# Redirect real URLs to "friendly" URLs
RewriteCond %{THE_REQUEST} \?id=([\w-]*)
RewriteRule ^page$ /%1? [R=302,L]
Change the 302 (temporary) to 301 (permanent) when you are sure this is working OK. Permanent redirects are cached by the browser so can make testing a problem.
So, in summary, with the above two parts shown together:
# Enable the rewrite engine
Options +FollowSymLinks
RewriteEngine On
# Redirect real URLs to "friendly" URLs
RewriteCond %{THE_REQUEST} \?id=([\w-]*)
RewriteRule ^page$ /%1? [R=302,L]
# Rewrite the "friendly" URL back to the real URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^id=
RewriteRule ([\w-]*) /page?id=$1 [L]
The order of directives is important. External redirects should nearly always come before internal rewrites.
UPDATE#1:
I want /concept?id=NAME to go to /NAME and /studio?id=NAME to go to /NAME - there's 5-10 different "pages" from both concept and studio. [Corrected according to later comment]
Since id=NAME maps to /NAME you can achieve all 10-20 redirects with just a single rule:
RewriteCond %{QUERY_STRING} ^id=(NAME|foo|bar|baz|abc|def|ghi)
RewriteRule ^(concept|studio)$ /%1? [R,L]
This will redirect a URL such as /studio?id=foo to /foo.
As with all external redirects this should be one of the first rules in your .htaccess file.
Change R to R=301 when you have tested that it is working OK.
To make this more "dynamic", ie. match any "NAME" then change the CondPattern, for example:
RewriteCond %{QUERY_STRING} ^id=([\w-]*)
UPDATE#2:
If the path part of the URL (ie. concept or studio) is required then you can modify the RewriteRule substitution like so:
RewriteCond %{QUERY_STRING} ^id=([\w-]*)
RewriteRule ^(concept|studio)$ /$1/%1? [R,L]
Which will redirect /concept?id=foo to /concept/foo.
Or, to be completely "dynamic" (bearing in mind this will now capture anything):
RewriteCond %{QUERY_STRING} ^id=([\w-]*)
RewriteRule ^([\w-]+)$ /$1/%1? [R,L]

htaccess mod rewrite confusion

I am trying to apply mod rewrites to my URL to make it nicer but I am getting caught up in all the confusion of this subject.
Please could you help me out with the following examples of what I want to do :
I want to attach any variable from the root of my site into the lyprofile.php page
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{THE_REQUEST} /(.*)
RewriteRule /(.*) lyprofile.php?us=$1
I want a url such as profile/settings to go to lysettings.php
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{THE_REQUEST} /profile/settings
RewriteRule /profile/settings lysettings.php
These two examples if working should help me to work out all my other URL's, but I can't get these to work.
Also do you need an absolute URL, as I'm working on my local machine and an absolute URL would just cause a lot of hassle. Just in case my absolute URL is http://localhost/Linku2.5/
You generally want to go from the most specific rules to the least specific. Of the two things that you want to do, the first is the least specific, as (.*) can be anything. So we have to start with the much more specific and less arbitrary /profile/settings.
If these rules are in your htaccess file (in your case, in the Link2.5 directory), you don't want a leading slash, so simply:
# you only need to turn on once
RewriteEngine On
# Don't need absolute URLs, but you may need this line:
RewriteBase /Link2.5/
RewriteRule ^profile/settings lysettings.php [L]
Then, because your other rule is so general, you need to add some conditions so that you don't cause an infinite loop (the rewrite engine will continue to loop until the URI stops changing):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ lyprofile.php?us=$1 [L,QSA]
If you know your "us" variable can only be numbers or letters, you can make the line with the rule a bit more specific:
RewriteRule ^([A-Za-z0-9]+)$ lyprofile.php?us=$1 [L,QSA]
etc.
Can you check this in vhosts config?
# Turn mod_rewrite on
RewriteEngine On
RewriteRule lyprofile\.php - [L]
RewriteCond %{REQUEST_URI} /(.*)
RewriteRule /(.*) lyprofile.php?us=$1 [L]
RewriteCond %{REQUEST_URI} /profile/settings
RewriteRule /profile/settings lysettings.php [L]

htaccess rewrite for subdomain only

I'm trying to rewrite some parameters to beautiful links, but for a subdomain / a folder only. Unfortunately I can't get it to work, maybe also because there are some other rewrites in line before...
Heres my code:
<IfModule mod_rewrite.c>
# NON-WWW TO WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# WORDPRESS-BLOG
Options +FollowSymlinks
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# REDIRECT FOR SUBDOMAIN
RewriteCond %{HTTP_HOST} ^subdomain.example.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)(?:/)?$ index.php?cshort=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)(?:/)?$ /index.php?cshort=$1&cid=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)(?:/.*)?$ /index.php?cshort=$1&cid=$2&step=$3 [L]
</IfModule>
Basically only the last part is the one I want to rewrite to change URLs from something like
http://subdomain.example.com/index.php?cshort=abc&cid=123&step=1 to http://subdomain.example.com/abc/123/1
The other rewriting rules for www.example.com shouldn't get affected. Unfortunately my current codes only does the first two rules for the blog and the www, but nothing happens on the subdomain. What's wrong in my code?
When you say that you want to rewrite from http://subdomain.example.com/index.php?cshort=abc&cid=123&step=1 to http://subdomain.example.com/abc/123/1 you mean that you want the user to enter the pretty URL and to have it serve the full URL in the background, not that you want to redirect from the ugly to the pretty URL, right?
In your RewriteRules, what are you trying to accomplish with "(?:/)?"? As written, that doesn't make any sense to me. If you're just trying to match whether or not the directory path ends with a slash, you can do that as follows:
RewriteRule ^([^/.]+)/?$ index.php?cshort=$1 [L]
EDIT: Additional suggestions:
Move the "Redirect for subdomain" section above the "Wordpress Blog" section. Since the Wordpress rule applies to "everything that's not a real file or directory, regardless of domain" that should go last.
RewriteConds only apply to a single RewriteRule that follows them. For each of the three rules you have listed under "Redirect for subdomain", after updating them per the above suggestion, you need to repeat the two RewriteCond lines in front of the RewriteRule.

Deny access to all files but index.php but allow GET variables

Basically I do not want people that visit my site to get all of the files, but all the things I tried and found on the internet disallow the usage of GET variables after the index.php. I'm using a rewrite to make domain.com/lol go to index.php?lol.
This is my current .htaccess file, if you'd like to modify it to make it easier for me, go ahead too.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule .? http://domain.com%{REQUEST_URI} [L,R=301]
RewriteRule ^act/(.*)$ index.php?act=$1
RewriteRule ^code/(.*)$ index.php?code=$1
RewriteRule ^login$ index.php?login
RewriteRule ^logout$ index.php?logout
RewriteRule ^add$ index.php?add
RewriteRule ^tac$ index.php?tac
RewriteRule ^profile$ index.php?profile
Following rule stops direct requests to index.php (either with or without) arguments:
# block direct requests to index.php and redirect it to /
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index.php$ /
If needed, you can change the rewrite target and/or add some more conditions based on what exactly is allowed and what's not.

Adding a new mod_rewrite rule, containing a generated ID, to an existing configuration

Yet another one I suppose...
How can I rewrite
http://domain.com/images/download/30
where 30 is a generated ID to
http://domain.com/pages/files/download.php?id=30
My current attempt looks like this:
RewriteRule .+images/download/(\d+) /pages/files/download.php?id=$1
Where am I going wrong here?
The rest of my .htaccess is:
# turn rewriting on
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^([^/\.]+)/?$ /index.php?page=$1 [L,NC,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?page=$1&var1=$2 [L,NC,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?page=$1&var1=$2&var2=$3 [L,NC,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?page=$1&var1=$2&var2=$3&var3=$4 [L,NC,QSA]
Before other rules add
RewriteRule ^images/download/(\d+)$ /pages/files/download.php?id=$1 [L]
and in /pages/ folder yo should switch off Rewrite Engine to avoid new rewrites on onther rules

Resources