Just wanted to know if is possible to use mod_rewrite on a single(or more ) sub folder(s).
have a service in a folder that i would like to call like a rest service syntax .
$url = 'http://mysite/subfolder/parem1/parem2/parem3/';
$_SERVER['REQUEST_URI']
$tokens = explode('/', $url);
echo $tokens[sizeof($tokens)-2];
so the question is if I could catch all the different segments in a index.php in the subfolder, by using forexample modrewrite ( by placing a .htaccess with the rules only in this folder ? ) or how is it done ?
regards
If you want to define rewriting rules for a subfolder, take a look at RewriteBase
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /yoursubfolder
RewriteRule ^(.*)$ index.php?$param=$1
</IfModule>
It is possible. In your .htaccess, you could put something like this:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ index.php?p1=$1
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2&p3=$3
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2&p3=$3&p4=$4
</IfModule>
In your case you could just put the .htaccess file with the index.php in the folder where your service is located.
This way you can fetch (and sanitize) the $_GET array, instead of fetching the whole url.
Related
Currently on my website, my profile urls look like this:
../user/?id=1
I would rather them look like this:
../user/1
How would I go about this? I'm running this with Apache and PHP if that is important to the question.
You can do this with .htaccess
Example:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^user/([a-zA-Z0-9_-]+)$ user.php?id=$1 [L,QSA]
</IfModule>
This htaccess rule will redirect input like www.site.com/user/123 to user.php?id=123
I have an URL that is https://www.myurl.com/subdir/language/login.php .
The "language" parameter is not an existing subfolder. The folder structure is:
root/subdir/.htaccess
As you can see the .htaccess file is located in https://www.myurl.com/subdir/.htaccess
I would like to do a .htaccess Rewrite Rule in order to change the called uri:
https://www.myurl.com/subdir/en/login (without .php)
to:
https://www.myurl.com/subdir/login.php?lng=en
My current apporach is:
# MultiViews must be disabled for the rewrite to work
Options -MultiViews
# Turn on the rewriting engine
RewriteEngine On
RewriteBase /subdir/
RewriteRule (en|de)(/login)$ login.php?lng=$1 [L]
But when I call the URI https://www.myurl.com/subdir/en/login I get an http error code 404.
Yes thats it! One more question, what if the scenario is all the same, but I would like to get the (en/de) in front of /subdir/ ? So the url would be myurl.com/en/subdir/login:
In this case rule must be placed in site root .htaccess instead of subdir/.htaccess with this rule:
Options -MultiViews
RewriteEngine On
RewriteRule ^(en|de)/(subdir/login)/?$ $2.php?lng=$1 [L,QSA,NC]
I have read lots of questions regarding URL rewriting with multiple parameters. However I still cannot fix this issue:
I have this URL with multiple parameters, some of them without a value
http://localhost:8888/coches/index.php?maker=all&model=®istration_year=&price_until=&tfseekfid=main_search&s=~
I would like to rewrite it to http://localhost:8888/coches/all-cars
I have tried this rule and several other ones but canĀ“t get it to work.
RewriteRule ^all-cars$ /index.php?maker=all&model=®istration_year=&price_until=&tfseekfid=main_search&s=~ [L,QSA]
Can anyone help? Thanks
Location of the .htaccess file containing this rule is important. You can have .htaccess files in sub-directories.
If the .htaccess file is in your docroot you need to specify the directory in your rule:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^coches/all-cars$ /coches/index.php?maker=all&model=®istration_year=&price_until=&tfseekfid=main_search&s=~ [L,QSA]
</IfModule>
If the file is /coches/.htaccess, remove the leading / from your rule to make it relative to the sub-directory:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^all-cars$ index.php?maker=all&model=®istration_year=&price_until=&tfseekfid=main_search&s=~ [L,QSA]
</IfModule>
I'm changing my website links to a SEO friendly urls, I did every thing in php file and changed the urls as following:
http://mywebsiet.com/news-details.php?id=2012/6/21/newstitle.html
how can I redirect to:
http://mywebsiet.com/2012/6/21/newstitle.html
I tried this Generating tool from www.generateit.net/mod-rewrite/
and created the .htaccess with the code provided:
RewriteEngine On
RewriteRule ^([^_]*)$ /news-details.php?id=$1 [L]
and even so, nothing get changed.... any ideas?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(.+?\.html)$ /news-details.php?id=$1 [L,QSA,NC]
For URL Rewriting, i have got the output for static URL
But , for dynamic URL , i am getting partial output only.
I have placed the .htaccess file in the root directory only.
Here is the code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^booking/price/([0-9]+)/?$ booking.php?price=$1
What will be the solution ?
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^booking/([0-9]+)/?$ booking.php?price=$1
RewriteRule ^booking/([0-9]+)/([0-9]+)/?$ booking.php?price=$1&pass=$2
You really want to customise your code for these though so as to be able to use general rules, rather than converting specific paths to use $_GET.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^booking/([0-9]+)/?$ booking.php?price=$1
RewriteRule ^booking/([0-9]+)/([0-9]+)/?$ booking.php?price=$1&pass=$2
For images and Css you need to define your base folder in htaccess file as
RewriteBase /~your folder/