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.
Related
I was looking for a simple .htaccess configuration that just convert /some_uri to /some_uri.php. Most examples in community are more complicated than I require. I was trying the following, but it didn't work:
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^(.*)$ ./$1.php
Please help. Thank you.
You need to make sure that the rule does not match itself. In other words, you need to make sure the rule does not match if the url already ends on php. Besides that you probably want to prevent that the rule matches if it already points to a file that exists, so that you can normally serve css/js/images without it trying to append php to it.
RewriteCond %{REQUEST_URI} !\.php$ #Does not end with .php
RewriteCond %{REQUEST_FILENAME} !-f #Requested url is not an existing file
RewriteRule ^ %{REQUEST_URI}.php [L]
I'm somewhat new to htaccess rewrite rules, and have been scratching my head for the past few days on what's happening here. No amount of Googling seemed to help, so hopefully somebody knows the answer.
I have a site that can be accessed as:
www.site.com
www.site.com/684
www.site.com/684/some-slug-name-here
All of these scenarios should go to index.php and pass in the optional id=684 and slug=some-slug-name-here
Which works fine.
My problem is I have a separate file. Right now it's called admintagger.php - but this fails when I call it anything. 21g12fjhg2349yf234f.php has the same issue.
The problem is that that I would like to be able to access admintagger.php from www.site.com/admintagger
but it seems to be matching my rule for index, and taking me there instead.
Here is my code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^imagetagger$ /imagetagger.php [NC,QSA]
RewriteRule ^([0-9]+)/?(.*)?/?$ index.php?id=$1&slug=$2 [NC,L,QSA]
If you want to arbitrarily be able to access php files via the name (sans extension) then you need to create a general rule for it. But you need to be careful otherwise you may be rewriting legitimate requests for existing resources (like a directory, or a slug). Try this instead:
# make sure we aren't clobbering legit requests:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# see if appending a ".php" to the end of the request will map to an existing file
RewriteCond %{REQUEST_FILENAME}.php -f
# internally rewrite to include the .php
RewriteRule ^(.*)$ /$1.php [L]
Then you can have your routing to index.php right after that:
RewriteRule ^([0-9]+)/?(.*)?/?$ index.php?id=$1&slug=$2 [NC,L,QSA]
Although you may be better off create a separate rule for each of your 3 cases:
RewriteRule ^([0-9]+)/([^/]+)/?$ /index.php?id=$1&slug=$2 [NC,L,QSA]
RewriteRule ^([0-9]+)/?$ /index.php?id=$1 [NC,L,QSA]
RewriteRule ^$ /index.php [L]
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]
i want to use somthing like http://www.example.com/domain.com instead of http://www.example.com/index.php?url=domain.com.
how can I do this using .htaccess?
update: i finally figured it out. :)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1
http://www.pagerankcheckertool.com/facebook.com
RewriteEngine on
RewriteRule ^(.+)$ index.php?url=$1
Depending on your needs, it may not be such a good idea to have everything rewritten as per your example, e.g. even a www.example.com/index.html would be rewritten to www.example.com/index.php?url=index.html so i'd recommend you use an initial sub folder or something in the url to seperate your rewritten urls from anything else.. i.e. www.example.com/urls/domain.com
To accomplish that you could setup a rewrite rule.. (assuming you have mod_rewrite active)
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^urls/(.+) /index.php?url=$1 [NC]
that basically means any url that begins with urls/ and has one or more characters following.. the brackets around the .+ will 'group' that element and allow you use it again with the $1
Hope that solves what you want to accomplish!
Hey guys, I'm trying to implement one of the answers I got to another question I asked on here a couple days ago. You can find the original question here: Mod_rewrite clarification question. Only for dynamic urls?
The most helpful answer and the one I'm modeling the implementation after is as follows:
I'm guessing the answer meder gave is the one you're looking for but technically you can create a static map file to redirect a set of title strings to ids and it doesn't have to execute an external prg executable:
RewriteMap static-title-to-id txt:/tmp/title_to_id.txt
RewriteRule ^/health-and-fitness-tips/(.*)/ /health-and-fitness-tips/${static-title-to-id:$1}/ [L]
with the contents of the /tmp/title_to_id.txt file something like this:
how-do-I-lose-10kg-in-12-weeks 999
some-other-title 988
and-another 983
Ok enough background. My .htaccess file currently has the following in it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Its a typical wordpress permalink customization. However, when I try to add rules similar to those presented in the selected answer above, I get an internal server error.
This is my .htaccess file when I get the internal server error:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteMap static-title-to-id txt:/tmp/title_to_id.txt
RewriteRule ^/health-and-fitness-tips/(.*)/ /health-and-fitness-tips/${static-title-to-id:$1}/ [L]
</IfModule>
I'm hoping my problem is something simple, like a rule conflict. If you can see an error or can provide some direction here, it would be much appreciated.
There are four errors in your code:
The RewriteMap direction can only be used in the server configuration or virtual host context.
When using mod_rewrite in an .htaccess file, mod_rewrites first removes the per-directory prefix from the URL path before testing the rules and reappended after applying a rule. In the case the .htaccess is in the root directory, the path prefix would be / that’s removed. So you need to specify the path pattern without that prefix:
RewriteRule ^health-and-fitness-tips/(.*)/ /health-and-fitness-tips/${static-title-to-id:$1}/ [L]
The substitution of your rule would also be matched by that same rule. That would lead to an infinite loop. So you need to either change the pattern or the substitution to avoid that. In your case changing the pattern would be better, for example:
RewriteRule ^health-and-fitness-tips/([a-zA-Z]+[a-zA-Z0-9]+|[a-zA-Z0-9]+[a-zA-Z]+)/ /health-and-fitness-tips/${static-title-to-id:$1}/ [L]
Your should also specify the end of the URL path:
RewriteRule ^health-and-fitness-tips/([a-zA-Z]+[a-zA-Z0-9]+|[a-zA-Z0-9]+[a-zA-Z]+)/$ /health-and-fitness-tips/${static-title-to-id:$1}/ [L]
As I assume that those URLs can not be directly mapped to existing files or directories, your first rule will catch them before you rewrite them. So you should change the order of that rules to have the specific rule applied before the catch-all one:
RewriteRule ^health-and-fitness-tips/([a-zA-Z]+[a-zA-Z0-9]+|[a-zA-Z0-9]+[a-zA-Z]+)/$ /health-and-fitness-tips/${static-title-to-id:$1}/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]