HTACCESS Rewrite Order - .htaccess

The line with rewriting index.php and the other three lines work perfectly, but not when put together, I've tried all possibilities with position of the lines, and to be honest pretty exhausted.
My code below:
RewriteEngine On
RewriteRule (.*) index.php
RewriteCond %{REQUEST_URI} !^/(admin|admin/.*)$
RewriteCond %{REQUEST_URI} !^/(l|l/.*)$
RewriteCond %{REQUEST_URI} !^/(includes|includes/.*)$
Can anyone help me in this quick question?
The script runs in: http://domain.com/folder/
The .htaccess is also placed in that folder.
The problem is that it rewrites also files in the admin,l and includes folder

I think what is happening here is the first RewriteRule (.*) index.php is set to rewrite everything regardless of if they match the other regex for the 3 RewriteCond's I think maybe moving it to the bottom and reformulating the rule to be more specific to your needs.

There are few problems in your Rewrite code:
RewriteCond should come before RewriteRule
Multiple RewriteCond can be combined into 1
You must use L flag to mark last rule
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(admin|l|includes)(/.*|)$ [NC]
RewriteRule ^ index.php [L]

Related

with .htaccess how do I remove slashes from some point?

I am trying to make a user system, where the URL is like:
test/user/[name]
It all works, but if you try:
test/user/name/something
It doesn't work. Is there any way to remove the slashes from that certain point, so that:
test/user/name/something
will go to:
test/user/name
How do I edit this, so it works as mentioned above:
RewriteEngine on
RewriteCond %{REQUEST_URI} user/(.*)
RewriteRule user/(.*) something/test/user.php?user=$1
You can place this rule as your first rule in your root /test/.htaccess:
RewriteEngine on
RewriteBase /test/
RewriteRule ^(user/\w+)/.+$ /$1 [L,NC,R]
RewriteRule ^user/(.+)$ user.php?user=$1 [L,QSA]

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]

change structure of url to directory-like structure

I know this question has been asked and answered many times, but my experience of .htaccess, regualr expressions, mod rewrites etc.. have normally drove me crazy.
I see on most websites the structure of the url is in a directory-like structure, wwww.linku.biz/edit. My ultimate question is how do you do this?
Are all these sites behind the scenes have normal URL variables but just re-wrote? such as www.linku.biz/myprofile?edit="whatever" , is this all done with .htaccess, and mod_rewrites?
I want to type in my url www.linku.biz/search however its actually www.linku.biz/search.php
I want to type in my url www.linku.biz/JackTrow however its actually www.linku.biz/profile.php?us="JackTrow
Also I want data re-wrote when I have lots of URL data, such as www.linku.biz/search?a=1&b=2&c=3 actually beeing www.linku.biz/search.php?a=1&b=2&c=3
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search\.php\[?^\s] [NC]
RewriteRule ^ search? [R=301,L]
RewriteRule ^search/?$ search.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ profile.php?us=$1 [QSA,L]
Please note that your requirement 1 & 3 will both be covered by rule # 1
Add this rules to your .htaccess file in Root folder :
To do this www.linku.biz/JackTrow <--- www.linku.biz/profile.php?us="JackTrow
RewriteRule ^profile.php?us=(.*)$ /$1 [R=301,L]
To do this : www.linku.biz/search however <--- www.linku.biz/search.php
RewriteRule ^(.*).php$ /$1 [R=301,L]
Your last one , i have not understood what you want exactly.

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.

Mod_rewrite /category.htm into /category

After all those mod rewrite topics here at stackoverflow, I still haven't find an answer to my question. I have a topsite, and basically all I want to do is to change /index.php?method=in&cat=Half+Life (the "+" is a space) into /Half-Life .
Until now, I've succeeded changing /index.php?method=in&cat=Half+Life into /Half+Life.htm .
What I want is to make the .htm disappear and to change the "+" into "-".
Here's my code with what I'm working on in my .htaccess file :
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)\.htm$ /index.php?cat=$1 [L]
One more question: Is it more, how should I call it, "SEO-friendly", if I do it this way?
Thanks!
The problem with removing the .htm from it is that it then rewrites every single url. That would include urls for files that actually exist (like index.php). There are a couple approaches that you could take.
Assume that all your actual files have a dot (.) in the file name, rewrite everything else. If you do actually have files or directories that are named without an extension, this won't work. Or if you want some of your categories to have dots.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ([^\.]+)$ /index.php?cat=$1 [L]
You can have your rewrite rule ignore files that actually exist
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?cat=$1 [L]
I think it would be easier to change the php to replace the dashes with spaces, but if you really want to use htaccess:
RewriteRule (.*)-(.*)\.(htm)$ "$1 $2.$3"
RewriteRule (.*)\.htm$ /index.php?cat=$1 [L]
What you seem to be asking is to translate SEO-style URIs to internal parameter based ones. Unfortunately, the rewrite engine doesn't support global substitution so you need to cludge this, for example:
RewriteBase /
RewriteRule (\w+)$ index.php?cat=$1 [L]
RewriteRule (\w+)+(\w+)$ index.php?cat=$1-$2 [L]
RewriteRule (\w+)+(\w+)+(\w+)$ index.php?cat=$1-$2-$3 [L]
# and so on ..
But the easiest way to do this is to use a general catch all:
RewriteRule ([-+\w]+)$ index.php?cat=$1 [L]
And use a str_replace or preg_replace on your $_GET['cat'] inside your inddex.php.

Resources