htaccess+modrewrite issue - .htaccess

I want that every user has a profile-url like this:
www.example.com/username
I have created this htaccess:
RewriteEngine on
RewriteRule ([^/]+) profile.php?username=$1 [L]
but I get error with the css and js files.
what I have to write in the htaccess ?

You should in general exclude all real files and directories, as this will handle css/js/images/whatever else you want to serve:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ profile.php?username=$1 [L]

Exclue js and css with RewriteCond.I think this should do:
RewriteEngine On
RewriteCond %{REQUEST_URI} ! \.(js|css)$ [NC]
RewriteRule ([^/]+) profile.php?username=$1 [L]
If you have images as well, add their extensions
RewriteCond %{REQUEST_URI} ! \.(js|css|jpg|gif|ico|png|whatever)$ [NC]

Related

Remove the .html extension in the URL, when the folder has the same name as the .html file

I have a problem with removing .html extensions from the website address. Everything works fine until I create a folder with the name that some file already has in the given location. For example, the situation with such files:
index.html
example1.html
example1/example2.html
When I try to go from index.html to example1.html, the page does not read the extension and shows me the folder tree instead of the page. It just goes to the example1 folder instead of example1.html. Anyone can help me? I've tried most (if not all) solutions to this problem with Google and nothing works :( I leave the content of .htaccess below. Thanks in advance
SetEnv PHP_VER 5_3
SetEnv REGISTER_GLOBALS 0
RewriteEngine on
# REMOVE .HTML
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
Okay, I found the answer to this question, just add these lines of code to .htaccess.
DirectorySlash Off
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(php|html)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*) /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*) /$1.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteRule ^(.*) %{REQUEST_URI}/ [L,R=302]

Setting up an htaccess file

I'm in the process of rebuilding a site and want to set it up as follows:
1 ) Pages other than index.html e.g. www.website.co.uk/support/web-design.html becomes www.website.co.uk/support/web-design/
2) Pages that aren't in subfolders e.g. www.website.co.uk/contact.html becomes www.website.co.uk/contact/
I know I should use a .htaccess file. I've looked various sites explaining htaccess and mod_rewrite but need help. Can anyone explain how this should this be setup to achieve the above?
Thanks
Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+([^\?]+)\.html
RewriteCond %1 !^index$
RewriteRule ^ /%1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]
These rules would go in the htaccess file in your document root
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^index\.html$ - [L,NC]
# To internally forward /dir/file to /dir/file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ /$1.html [L]

Redirect url if it has specific extension

How can I redirect URLs which has extension like .css or .js but not .php to specific folder.
I mean all the URLs which has extension but their extension is not php should be redirect to specific folder.
I try to do it like this but it does not work:
RewriteCond %{REQUEST_URI} \.(.{2,4}!php)$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)\.(.{2,4}!php)$ folder/$1.$2 [L]
Well I have found the solution:
RewriteCond %{REQUEST_URI} \.(.{2,4})$
RewriteCond %{REQUEST_URI} !\.(php)$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ $1 [L]
First I check if the extension is in desire length, then I check it is not end with .php
Your regex isn't right, you want simply !\.php$:
RewriteCond %{REQUEST_URI} \.\w{1,4}$
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ folder/$1 [L]

htaccess file misconfigured

I am using codeigniter php framework. I am trying to access a folder under public_html, but I can't get to it. It shows 404 custom page by codeigniter. Does the following script in .htaccess has something to do with it?
RewriteEngine on
RewriteCond $1 !^(index\.php|js|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Thanks
It probably is, you can try adding either some conditions:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|js|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Or include your folder as part of the exclusion (example, your folder is "foobar"):
RewriteEngine on
RewriteCond $1 !^(index\.php|js|images|robots\.txt|foobar)
RewriteRule ^(.*)$ /index.php/$1 [L]

Set language using htaccess URLrewrite

I have for example two files in my root directory: one.php and two.php. I would like to reach them through these urls without having to actually have the physical directories en+de.
mydomain.com/en/one
mydomain.com/en/two
mydomain.com/de/one
mydomain.com/de/two
So the first directory would be "ignored" and just pass a GET variable with the language setting to the php-file, second directory is the php-file without extension.
This is what I'm using in my htaccess to loose the file extension so far:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
UPDATE/SOLUTION
This is quite useful: .htaccess rule for language detection
and this seems to pretty much do what I wanted:
RewriteRule ^(en|de|fr)/(.*)$ $2?lang=$1 [L,QSA]
RewriteRule ^(.*)$ $1?lang=en [L,QSA]
ONE MORE ADDITIONAL QUESTION
For mydomain.com/en/one/id45
If I wanna pass id45 as a variable to one.php what line in htaccess do I have to add?
You can try this rule:
DirectoryIndex index.php index.html
DirectorySlash On
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$2.php -f
RewriteRule ^(en|de|fr)/([^/]+)(?:/(.*)|)$ $2.php?lang=$1&var=$3 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/?$ index.php?lang=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ $1?lang=en [L,QSA]

Resources