I'm trying to implement some htaccess rewrite rules. However in not a pro in htaccess.
I have a subdomain gateway in (as described in: .htaccess and rewrite sub domains)
Now i would like to use SEO friendly URL's. The gateway has some subfolders i.e. email in this case.
http://gateway.example.com/email/Param1/Param2/
This url should be rewritten to http://gateway.example.com/email/index.php?hash=Param1&hash2=Param2.
In the email folder I have created a .htaccess file with the following contents:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.* - [L]
RewriteRule ^/(.*)/(.*)/ index.php?hash=$1&hash2=$2
When requesting the http://gateway.example.com/email/Param1/Param2/ i'm now getting a 404. What is wrong with the rule?
You should do it this way
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([^/]*)/([^/]*)/ /email/index.php?hash=$1&hash2=$2 [L]
you can try this.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^/([^/]*)/([^/]*)/ index.php?hash=$1&hash2=$2
*you need to configure apache(httpd) to accept UrlRewrite by changing AllowOverride from non to all like this :
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
Related
I Have the next configuration
<VirtualHost *:443>
ServerName projectsite.com
ServerAlias projectsite.com
DocumentRoot /var/www/project
<Directory />
RewriteEngine On
Options +FollowSymlinks
AllowOverride All
Require all grant
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/?.+$ / [L]
</Directory>
</VirtualHost>
The project manage all urls from the client, so, I only need to return the index.html file and I render the modules with javascript. It works ok but If the url matches with a folder then I am getting a 403 error "Forbidden". Instead of this, I need return the root index.html file either is the url matches with a directory name or not.
¿How can I do it ? I tried to find on internet but I didn't found something similar.
UPDATED:
how Dusan Bajic told me, I removed that line RewriteCond %{REQUEST_FILENAME} !-d and it resolved the first problem, but now the Rule is adding a slash at the final of the url,
If I have "site.com/reports" it changes to "site.com/reports/". I need to mantain the url without this slash. I think the problem is with the
next Rule:
RewriteRule ^/?.+$ / [L]
But i don't know the correct way to make it
How #dusanBajic commented I had a rule that prevented what I need.
The rule is :
RewriteCond %{REQUEST_FILENAME} !-d
This rule indicates that the rewrite rule only apply if the folder does not exists. So what I did was Remove that instruction and add the next rule DirectorySlash Off to prevent apache puts a slash at the final of the url.
The code left like this:
<Directory />
RewriteEngine On
Options +FollowSymlinks
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/?.+$ / [L]
</Directory>
i use friendly url names like myservice/controller/action/key/value/key/value etc. Everything is ok when i am running my server on remote hosting. But now i tried run server on my linux and i have a access by ip. My mod_rewrite rules doesn't work. Could someone tell me how can i change this ?
my old rules working on remote hosting with domain
rewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Options +FollowSymLinks -Indexes -MultiViews
And this is my new rules not working
rewriteEngine On
RewriteCond %{REMOTE_ADDR} !^192\.168\.1\.104$
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Ok i tried many times and this rules works fine
rewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Options +FollowSymLinks -Indexes -MultiViews
But in vhost in apache2/available-sites i had to add
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
Thanx for help
My yii project has following structure:
webapp
----frontend
--------www
------------index.php
----backend
--------www
------------index.php
----api
--------www
------------index.php
https://github.com/tonydspaniard/yiinitializr-advanced
Apache 2.2
Each www directory has own .htaccess file. For example "webapp/frontend/www/.htaccess":
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/].*)$ /index.php/$1 [L]
For separate parts of application i use subdomains "api.webapp.com","admin.webapp.com" (symlinks in public_html folder):
webapp.com -> webapp/frontend/www
api.webapp.com -> webapp/api/www
admin.webapp.com -> webapp/backend/www
But i have problems with cross domain ajax requests to api, and i want to get something like this:
http://webapp.com -> webapp/frontend/www/index.php
http://webapp.com/api/ -> webapp/api/www/index.php
http://webapp.com/admin/ -> webapp/backend/www/index.php
What is the right way to route URL segment to specific web root directory? Aliases, .htaccess, symlinks, maybe something else?
As aCodeSmith said, you can properly configure .htaccess file to solve the issue.
The document root of http://webapp.com should be webapp.
Right in the document root you should create .htaccess file with route rules like that.
RewriteCond $1 ^api\/
RewriteRule ^(.*) api/www/index.php [L,PT]
RewriteCond $1 ^admin\/
RewriteRule ^(.*) admin/www/index.php [L,PT]
RewriteRule . frontend/www/index.php
Hope, it will help.
Thanks, guys.
Now i have two working solutions:
1) by Alex Akr
webapp is root
webapp/.htaccess:
RewriteCond $1 ^api\/
RewriteRule ^(.*) api/www/index.php [L,PT]
RewriteCond $1 ^admin\/
RewriteRule ^(.*) admin/www/index.php [L,PT]
RewriteRule . frontend/www/index.php
2) webapp/frontend/www is root.
In apache config set aliases:
Alias /api /var/www/webapp/data/www/webapp/api/www
<Directory /var/www/webapp/data/www/webapp/api/www>
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
In /webapp/api/www/.htaccess
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule ^([^/].*)$ /api/index.php/$1 [L]
As an experienced web developer, I feel like an idiot posting this. Somehow I tend to have issues with .htaccess. I'm trying to route all requests within /wiki to my index.php with the following...
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ./ /index.php [L]
Virutal host:
<VirtualHost *:80>
ServerName mysite.local
DocumentRoot "/var/www/mysite/public_html"
<Directory "/var/www/mysite/public_html">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
I'm trying to access http://mysite.local/wiki/asdf and getting a 404 error.
Apache's error log shows nothing
If /wiki is an existing folder in your public_html root folder, and your index.php file is in the wiki folder, then you can give the following a try:
RewriteEngine On
RewriteBase /wiki
RewriteRule index\.php - [F,L]
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule .* index.php [L]
If the wiki folder does not exist, and you're doing all the work at the root:
RewriteEngine On
RewriteBase /
RewriteRule index\.php - [F,L]
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule wiki/(.*) index.php/$1 [L]
Now give this a try! To rewrite non-existence /wiki and /wiki/ and /wiki/$var into /index.php:
RewriteCond %{REQUEST_URI} ^/wiki/?
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /index.php
I ended up needing what the Yii Framework uses...
RewriteEngine On
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
I am trying to remove php extension from url, But its not working for me. I am using Windows 7, with apache2.2.17 and php5.3. Now I have configured it as
in .htaccess file
OPTIONS -Indexes
IndexIgnore *
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
in httpd.conf enabled mod_rewrite as
LoadModule rewrite_module modules/mod_rewrite.so
and
<Directory "D:/Apache/htdocs">
Options Indexes FollowSymLinks ExecCGI MultiViews
AllowOverride all
Order allow,deny
Allow from all
</Directory>
in php.ini
expose_php = On
After making these configuration still i am not able to remove php extension from url in my windows machine but same thing is working in ubuntu server.
Is there anything I am missing here?
This might answer your question:
RewriteRule ^/([^-]*)$ /$1.php
It works for root catalog.
RewriteRule ^catalog/([^-]*)$ catalog/$1.php
Or you can write automatic script:
RewriteRule ^/([^-])/([^-])$ /$1/$2.php
But this is for "httpd.conf" file.
Where: ([^-]*) means variable; $1, $2 means converted to variable i.e.
www.verity.com/foo/bar => verity.com/foo/bar.php
Use this instead:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php
By doing this, you will be able to call example.com/abc.php as example.com/abc