How-to RewiteRule for two scenario files? - .htaccess

i have such .htaccess:
RewriteEngine On
# http://site.com/ru,ua,lt/anything/
RewriteRule ^(ru|ua|lt)/([^/]+)/$ index.php?lang=$1&article_id=$2
# http://site.com/print/ru,ua,lt/anything/
RewriteRule ^print/(ru|ua|lt)/([^/]+)/$ print.php?lang=$1&article_id=$2
First example(# http://site.com/ru,ua,lt/anything/) works, second not work. Please help solve situation.

First of all change your code to this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# http://site.com/ru,ua,lt/anything/
RewriteRule ^(ru|ua|lt)/([^/]+)/?$ index.php?lang=$1&article_id=$2 [L,NC,QSA]
# http://site.com/print/ru,ua,lt/anything/
RewriteRule ^print/(ru|ua|lt)/([^/]+)/?$ print.php?lang=$1&article_id=$2 [L,NC,QSA]
Then try out your URIs and report back if it still doesn't work.

What does not work? Is it only external files (JS, CSS, Images) that do not work?
This could be the case, for your second URL has an additional sub directory. You should then use absolute URL paths to link to external files.

Related

.htaccess simulate directories

I want to redirect /services/websites.html to just websites.html in my head directory. That way I will be able to put all my files in the head directory so all css files and stuff like that will be linked correctly. I tried:
RewriteRule ^services/websites.html$ websites.html
But that does not work. I figured it is something simple but I couldn't find anything on Google.
Could you help me?
This is the code you need to put in your .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^services/(.*)$ $1 [L,NC]
try:
RewriteRule ^.*websites\.html$ websites.html
Please note the html not hmtl in your example.

Remove file extensions using htaccess in subdirectories

I'm trying to remove file extensions with htaccess, so www.mysite.com/file.php becomes www.mysite.com/file.
I'm using the following code in the .htaccess file:
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
So far, so good. Where it falls down is in subfolders: www.mysite.com/subfolder/file.php
becomes www.mysite.com/file.
I've tried uploading another .htaccess file in the subfolder, but it still does the same. It feels like it should be really simple, but I'm struggling...can anyone help me out? Thanks!
Edit Sorry folks, should have said - the file is in a subfolder like so:
www.mysite.com/folder/subfolder/file.php
The .htaccess file is in /folder, the URL changes to this format:
www.mysite.com/subfolder/file
Apologies for misleading.
This is the rule you'll need to hide .php extension. This goes into your .htaccess in the DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
You shouldn't be using rewrite rules for this. Apache has built-in option explicitly for doing what you're trying to do, called MultiViews, and that's what you should be using:
The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.
Just add Options MultiViews to your .htaccess, and make sure that AllowOverride is properly configured.
A guess in the wild
Try
RewriteRule ^(.+)$ $1.php [NC,L]

PHP page access without extension and variable

How can I create a PHP page system that we access without .php extension and without ?page= variable?
There's a simple exemple:
http://www.exemple.com/portfolio/1
Instead of:
http://www.exemple.com/portfolio.php?id=1
It would be great for me because I'm a web designer and this is a thing I never did. So it would be good to know this! Also I will use this in my website.
you would need the following in your .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*[^/])/?$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule .+ %1.php [QSA,L]
You're looking for mod_rewrite.
RewriteEngine On
RewriteRule ^/profile$ /account.php?profile [L]
Create .htaccess file in your web root and enter following.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^account/([0-9]+)$ account.php?id=$1
OR
See mod_rewrite, in case URL rewriting, if you trying to do such.
You can use multiviews, or better and more used: mod_rewrite module from apache. It can be via a .htaccess file where you create rewrite rules like
RewriteEngine On
RewriteRule ^(.*)?(.*)$ /$1.php?Action=$2 [L]

How to rewrite numerous URL's at once?

I have a series of price lists from various manufacturers (spat out from a database). They are limited to 100 rows per page, ie:
www.domain.com/products/price-lists/company1.php?page=2
www.domain.com/products/price-lists/company2.php?page=10
which i want to rewrite to:
www.domain.com/products/price-lists/company1/page/2
www.domain.com/products/price-lists/company2/page/10
I have:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^products/price-lists/company1/page/([0-9]+)/?$ products/price-lists/company1.php?page=$1 [NC,L]
RewriteRule ^products/price-lists/company2/page/([0-9]+)/?$ products/price-lists/company2.php?page=$1 [NC,L]
First of all, this throws a 404 if i try to go to /products/price-lists/company1/page/2 (it works if i remove /page/) and second, i was wondering if i could combine them into one rule anyway:
(company1|company2[0-9]+)/?
Thanks for any help (this is my first mod_rewrite attempt!)
Try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^products/price-lists/([a-z0-9_\-]+)(/page)?/?$ products/price-lists/$1.php?page=1 [NC,L]
RewriteRule ^products/price-lists/([a-z0-9_\-]+)/page/([0-9]+)/?$ products/price-lists/$1.php?page=$2 [NC,L]
The first rule will check if a page is there or not without a number and go to page 1. The ([a-z0-9_\-]+) will be use to put the company, so if you put:
http://www.domain.com/products/price-lists/stackoverflow/page/2
the /products/price-lists/stackoverflow.php will open using $_GET['page'] = 2
The problem is that the part of the url is the same as the filename of the php file (without the .php). Use the following to correct.
options -multiviews

URL rewrite using mod_rewrite in .htaccess

I am trying to rewrite the URL using the mod_rewrite apache module.
I am trying to use it as below :
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm wants.php?wantid=$1 [L]
I have created a .htaccess file in the directory from where the files are accessed.
The mod_rewrite is also enabled.
With all these done, i still haven't been able to get it working.
Can someone please help me with this? Please let me know if i am missing something
Thanks in Advance,
Gnanesh
As per OP's comment:
The URL shows
mydomain.com/wants.php?wantid=123. I
need to make it look like
mydomain.com/wants/123
This should work for your case:
RewriteEngine on
RewriteBase /
RewriteRule ^wants/([0-9]+)$ /wants.php?wantid=$1 [L]
It will allow you to use http://yoursite.com/wants/123 and will silently rewrite it to wants.php?wantid=123
I think a leading slash (or rather the lack thereof) might be yoru problem:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm /wants.php?wantid=$1 [L]
Otherwise Apache might be looking for the PHP file in /wants/wants.php as it'll be treating it as a relative URL.
Hmm, so I gave it some thought and I guess you could do something like this ( only changed the regexp according to your comment, if I understood correctly ):
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/\d+$ /wants.php?wantid=$1 [L]
But I guess you could also leave out the $1 reference, and still be able to access the id in your wants.php. So
RewriteRule ^wants/\d+$ /wants.php [L]
should work too, and you can then use something like
<?php
$request = split('/', $_SERVER["REQUEST_URI"])[1];
?>
in your wants.php where the last element of the array would be your id ( or anything else you ever decide to rewrite and send to the script ).
If you want to use the rule in the .htaccess file in your wants directory, you have to strip the contextual wants/ from the start of the pattern. So just:
RewriteEngine on
RewriteRule ^([0-9]+)$ /wants.php?wantid=$1 [L]
Otherwise, if you want to use the rule in the .htaccess file in your root directory:
RewriteEngine on
RewriteRule ^wants/([0-9]+)$ wants.php?wantid=$1 [L]

Resources