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]
Related
I am not a professional but with help from SO I was able to rename the profile page URL's for users on my project using Rewrite_mod using:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1 [QSA]
This works good. But I can't figure out how to give a specific name to a particular page. For example I have a page profile_settings.php when users go to that page I want it to be example.com/settings in the address bar. Tried few ways from SO like removing .php thought it will remove php extension for all files but It did not work at all. Now my .htaccess looks like:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Was hoping example.com/profile_settings.php to be example.com/profile_settings but it did not work it's still full address with file extension. Moreover It will be good if I can deal with single file separately as besides removing extension I also want it to give desired name.
Rewrites for specific cases like /settings -> /profile_settings.php need to be handled separately and before other generic rules like this:
RewriteEngine On
# specific rewrite rules
RewriteCond %{THE_REQUEST} /profile_settings\.php[\s/?] [NC]
RewriteRule ^ /settings [R=301,L]
RewriteRule ^settings/?$ profile_settings.php [L,NC]
# add .php extension if corresponding .php file exists
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# handle profile page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [QSA,L]
I am trying to rewrite the following URL via .htaccess:
http://website.com/dealer_home.php?id=dealer1
The result I am aiming for is this, where dealer1 is the username which is set as variable:
http://website.com/dealer1
I have tried this rule in .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /dealer_home.php?id=$1 [L]
However I get "Internal Server Error" message when trying to load any of the website pages.
Can you provide some advice where I am doing wrong?
EDIT:
I tried also RewriteRule ^(.*)$ dealer_home.php?id=$1 [PT] but no success.
Thank you!
Maybe it's a conflict with existing files/folders and root uri.
Try this code instead
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]
This rule will match every url like domain.com/something if something is not an existing file or folder.
So if you have other rules then you should put them above this one.
EDIT: to avoid duplicate content and to redirect old format to new url format
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/dealer_home\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]
Unlike most htaccess requests, where I can simply use the following code to grab the entire URL.
RewriteRule ^(.+)$ /index.php?url=$1 [L,QSA]
What I need to do, is add paging without the use of a directory ( /page/# ) in the address. So unlike /page/# in the url, the paging is just adding a dash and a number at the end of the url, such as " -3 for page three, etc.
I've tried several rewrite Rules, but I don't believe I understand apache rewriting well enough because my regular use of paging, doesn't work when applying a catchall expression.
RewriteRule ^(.+)-([0-9]{1,5})$ /index.php?do=something&url=$1&page=$2 [L] ## Error In? (.+)?
RewriteRule ^(.+)$ /index.php?do=something&url=$1 [L,QSA] ## Works
In the other urls, which contain direct directories such as "something", the paging will work fine.
RewriteRule ^something/([a-z]{1,6})/(.*)-([0-9]{1,5})$ /index.php?do=first&what=$1&url=$2&page=$3 [L]
What do I have to fix in the code for catchall paging. What am I missing? The address WILL have dashes, and sometimes slashes for directories.
ERROR Code:
RewriteRule ^(.+)-([0-9]{1,5})$ /index.php?do=this&url=$1&page=$2 [L]
Currently looks exactly like the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)-([0-9]{1,5})$ /index.php?do=lists&url=$1&page=$2 [L]
RewriteRule ^(.+)$ /index.php?do=lists&url=$1 [L,QSA]
If I remove The line "RewriteRule ^(.+)-([0-9]{1,5})$ /index.php?do=lists&url=$1&page=$2 [L]" - everything works fine.
RewriteCond is only applicable to very next RewriteRule. Try this code:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(.+?)-([0-9]{1,5})$ /index.php?do=lists&url=$1&page=$2 [L,QSA]
RewriteRule ^(.+)$ /index.php?do=lists&url=$1 [L,QSA]
AHh I see now. The RewriteCond before makes ALL the difference. A set of rewrite conditions only applies to a single redirect rule. You may need to do this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)-([0-9]{1,5})$ /index.php?do=lists&url=$1&page=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?do=lists&url=$1 [L,QSA]
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]
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