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]
Related
I'd like to ask if it's possible to use a dynamic url and GET at the same time.
Let's say my current dynamic url is: https://yourdomain.com/blog/this-is-a-title
Would it be possible to make this work too: https://yourdomain.com/blog/this-is-a-title?action=delete
RewriteEngine on
RewriteRule ^([0-9]+)$ index.php?id=$1
The dynamic url mentioned first works fine, but I want to make the second work as well.
This is my .htaccess - hope it helps.
PS: I know that the regex in my htaccess isn't correct, it's just an example.
Have your .htaccess Rules file in following manner. Please make sure that your htaccess Rules file is present in root folder(where blog and htaccess both are residing in it; htaccess shiouldn't be inside blog folder; should be place same folder with it). Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Newly added rules here...
RewriteBase /blog/
RewriteCond %{THE_REQUEST} \s/blog/(?:[^?]*)?action=(\S+)\s [NC]
RewriteRule ^ index.php?action=%1 [L]
##Old Rules OP's htaccess ones.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)$ index.php?id=$1
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]
Firstly, sorry for my bad English.
I want config my .htaccess to rewrite URL.
example.com/company1.com
instead example.com/sub=company1.com
My .htaccess now:
RewriteEngine On
RewriteRule ^([a-z_]+)/?$ index.php?sub=$1
I was search in stackoverflow.
If i using (.*) regex for all charaters or ([a-z\.]+) for include "dot" character in domain string ( company1.con), my skin was broken.
My temporary solution is use ([a-z_]+) with http://example.com/company1_com instead
http://example.com/company1.com
It's bad solution :(
So, please give me regex for this problem.
Thanks.
Rewriting for Apache is described in mod_rewrite.
For you, as long as you ignore possible GET-parameters or paths, it should be
RewriteEngine On
RewriteRule ^/([^?/]+) /index.php?sub=$1 [L]
I guess it was broken because either you were missing the "/" before index.php, there is a longer path in GET ( example.com/company1.com/css/style.css ) or you submit a form ( example.com/company1.com?a=foo&b=bar ).
You need to prevent the index.php from looping:
RewriteEngine On
# let index.php pass through, thus stopping the rewrite loop
RewriteRule index.php - [L]
# route everything to index.php
RewriteRule ^(.*)$ /index.php?sub=$1 [L]
You could also do a check for existing resources first. Since index.php exists, that would also break the loop. This would make it so if you're requesting static content like javascript or css, it won't get routed through index.php:
RewriteEngine On
# request isn't for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# route everything to index.php
RewriteRule ^(.*)$ /index.php?sub=$1 [L]
Try this one
RewriteEngine On
RewriteRule ^(.*)$ index.php?sub=$1
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.
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!