I made a website and placed this code in the .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (.*)$ profile.php?id=$1 [QSA,L]
RewriteRule (.*)$ category.php?id=$1 [QSA,L]
Now, the first Rewrite rule works as expected, allowing me to have urls like site.com/profile instead of site.com/profile.php?id=foo.. but, when I added the second one, to achive the same result as the above solution, the page breaks down, and the css does not get included. So, it seems that the two can not be writen together, and I don't know what the solution is.
Try changing to:
RewriteRule ^profile/(.*)$ profile.php?id=$1 [QSA,L]
RewriteRule ^category/(.*)$ category.php?id=$1 [QSA,L]
Related
forcing this to work makes my kind of crazy so i hope you can help.
I use Rewrite Rules and .htaccess to make my dynamic URL
example.com/page.php?id=1
look like this
example.com/1
using
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
, and it works perfectly fine so far.
But i also want to hide the filetype in the URL ( impressum.php to impressum) using
RewriteRule (.*) $1.php [L]
So both Rules are working completely correct as long as i dont use them both at the same time. When i do so, which looks like this (my complete file)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
,i get an Internal Server Error. I tried different versions, for example change the positions and so on, but i allways get this error.
So my question is: how do i get both rules together and working, while the URL ending is still hidden and the example.com/1 works too?
Thank you very much for any answer
You can use the following in your .htaccess file:
RewriteEngine on
# Check if the PHP file exists and route accordingly
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
# If not, pass the request to page.php if it contains A-Za-z0-9-
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9-]+)/?$ page.php?id=$1 [NC,L]
You need two separate rules. Rewrite conditions will only get applied to the immediately following rule and with your php extension rule, you must check that the php file exists before adding the php to the end:
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
Hoping someone can help me here. In my .htaccess file, I've got a rewrite rule written as:
RewriteRule ^/?wrestler/([^/]*)$ /wrestler.php?id=$1 [L]
for a URL that would be something like
localhost/wrestling.php?id=something
And now what I'm trying to do is change a URL like
localhost/category.php?id=something
to
localhost/id/category
The thing to note with this scenario is that this time I'm trying to use the id part as the thing after the first "/" and then add the category part after the second "/".
Below is what my entire .htacces file looks like at the moment.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?article/([^/]*)$ /article.php?id=$1 [L]
RewriteRule ^/?wrestler/([^/]*)$ /wrestler.php?id=$1 [L]
# Removes the .php extension from pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
Any thoughts guys?
You can try
RewriteRule ^([^/]*)/category/?$ /category.php?id=$1 [L]
Try this :
RewriteRule ^([^\/]*)/([^\/]*)$ /$2.php?id=$1 [NC,QSA]
You need two captured parameters.
This expression was tested with http://htaccess.madewithlove.be/.
I need help with this rewrite in .htaccess file.
So this what I have now ans this works but when I try to add a new RewriteRule nothing happens.
I the url that I want to be rewrite is index.php?page=$1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
So when I do it like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ index.php?page=$1
The page doesn't have any css when i do it like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*_)$ index.php?page=$1
The page has css but i still get index.php?page=pagetitle. But the profile page does give me /username.
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ index.php?page=$1
Your are asking the server to redirect every URL to two different pages, it cannot work the server cannot just guess what page to load.
What you need is either a /profile/username rule or a /page/pagetitle rule.
IE something like:
RewriteRule ^profile/(.*)$ profile.php?username=$1 [QSA]
RewriteRule ^(.*)$ index.php?page=$1 [L]
Your rewrite rules are based on regular expressions and therefore need to be as specific as possible so the server can determine exactly which url to use - for example how can you tell if http://example.com/something is a page or a profile? Using a prefix such as "user", "profile", etc on your URLs means that http://example.com/profile/something can be redirected as as a username with a default redirect for everything else. To accomplish this you need to make the more specific pattern match first (users) and utilized the [L] directive to indicate that following rules should not be processed. I usually use a negative character class for URLs to match anything except a forward slash - [^/]*.
# Enable mod_rewrite
RewriteEngine On
# Set the base directory
RewriteBase /
# Don't process if this is an actual file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Does this url start with /profile and then followed with additional characters?
RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]
# Assume everything else is a page
RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
Test at http://htaccess.madewithlove.be/ (note that %{REQUEST_FILENAME} and %{REQUEST_FILENAME} aren't supported for testing).
Profile
input url
http://www.example.com/profile/something
output url
http://www.example.com/profile.php
debugging info
1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,QSA,L]
This rule was met, the new url is http://www.example.com/profile.php
The tests are stopped because the L in your RewriteRule options
2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
Page
input url
http://www.example.com/something
output url
http://www.example.com/index.php
debugging info
1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]
2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
This rule was met, the new url is http://www.example.com/index.php
The tests are stopped because the L in your RewriteRule options
I have url as
http://www.mydomain.com/levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c. I need to change this url to http://www.mydomain.com/medium.
I am not familiar with rewrite url.
I tried with RewriteRule ^medium/?$ levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c, but not worked correctly.
Full rewrite rule
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^medium/?$ levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
RewriteRule ^(.*)$ index.php [QSA,L]
Try to change only that line to:
RewriteRule ^medium /levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
Or if you don't care if anything is after medium is the following:
RewriteRule ^medium(.*) /levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
Your rules are in the wrong order, try changing them to:
RewriteEngine On
RewriteRule ^medium/?$ levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
The conditions need to be applied to the index.php rule, and not the medium rule. RewriteConds are only applied to the immediately following RewriteRule, not all of them.
This rewrite rule worked fine
RewriteRule ^medium.*$ /levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c [R=301,L]
I want to be able to handle mod rewrites from within PHP instead of my .htaccess file, this way when I have custom modules that need a new rewrite I don't have to redo the .htaccess file.
I want to mimic the way that WordPress does their .htaccess which is:
RewriteEngine On RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php/$1 [L,QSA]
My current .htaccess is this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# MBP Rules
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=module&prefix=$1&module_page=$2 [NC,QSA,L]
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=module&prefix=$1&module_page=$2&page=$3 [NC,QSA,L]
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/([0-9]+)/?$ /index.php?p=module&prefix=$1&module_page=$2&id=$3 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/?$ /index.php?p=$1 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=$1&s=$2 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/([A-Za-z-_]+)/([0-9]+)/?$ /index.php?p=$1&s=$2&id=$3 [NC,QSA,L]
Does anyone know how to make this happen?
My way is to replace
RewriteRule ^(.+)$ /index.php/$1 [L,QSA]
With
RewriteRule ^(.+)$ /index.php?path=$1 [L,QSA]
Then you have to do similar parsing and everything as modrewrite does, but you can do it yourself using preg_match on $_GET['path']. I.e.
if (preg_match('/id/([0-9]*)', $_GET['path'], $matches)) {
Do code;
}
Sure, have the .htaccess file look like the first example you have, and build your script from there.
Probably write some sort of Router class, to route the requests to their places, the fundamentals being splitting the received query by /, which gives you an array of URL parts, and start conditioning.