Please help me with this one, which should be easy but I've never managed to properly learn Apache mod_rewrite's syntax...
I have a REST webservice implemented in PHP and I need to rewrite the following URL:
[1] http://www.myserver.com/service/ca;x={valx},y={valy},z={valz}
into
[2] http://www.myserver.com/service/ca.php?x={valx}&y={valy}&z={valz}
How to accomplish this?
I'm using Apache2 on Ubuntu, this is the configuration:
Alias "/service" "/opt/htdocs/service"
<Directory "/opt/htdocs/service">
AllowOverride All
Options -Indexes FollowSymLinks
</Directory>
and the content of my /opt/htdocs/service directory:
$>ls -1 /opt/htdocs/service
ca.php
Mod_rewrite is enabled:
$>a2enmod rewrite
Module rewrite already enabled
Thanks in advance!
Try putting this in your server/vhost config or the htaccess file in your document root:
RewriteEngine On
RewriteRule ^/?service/ca;(.*)$ /service/ca.php?$1 [L]
RewriteCond %{QUERY_STRING} ^(.*),(.*)$
RewriteRule ^/?serivce/ca\.php$ /service/ca.php?%1&%2 [L]
Related
I have two problems actually:
First, I’m trying to redirect several short URLs to a single page with more actions, like this:
RewriteEngine On
RewriteRule ^/login?$ ^login.php?action=login&next=$1 [L]
RewriteRule ^/reset?$ ^login.php?action=reset&next=$1 [L]
This is being written in the .conf file inside <Directory>. The problem is that the first rule gets executed, while the second doesn’t and I can’t figure why.
I also tried writing them like this:
RewriteCond %{REQUEST_URI} /login$
RewriteRule ^login.php?action=login&next=$1 [L]
RewriteCond %{REQUEST_URI} /reset$
RewriteRule ^login.php?action=reset&next=$1 [L]
I should probably mention that login.php does not reside in the root directory, but in different subdirectories.
What am I doing wrong and how can I fix it?
The second issue I have is that if I put an .htaccess file inside the root directory, the rules in the .conf file don’t get executed anymore.
Inside the .conf file I have these rules:
<Directory>
Options Indexes FollowSymLinks ExecCGI Includes MultiViews
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
</Directory>
Why is this and how can I fix it?
Just to extend on from my comments above. Place these rules in your site root .htaccess or in httpd.conf file:
Options -MultiViews
RewriteEngine On
RewriteRule ^/?login(?:/(.*))?$ subdir1/login.php?action=login&next=$1 [L,NC,QSA]
RewriteRule ^/?reset(?:/(.*))?$ subdir1/login.php?action=reset&next=$1 [L,NC,QSA]
Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.
The objective is to input an url like
https://www.mywebsite/expert/188/name-of-the-expert
and return it to the server in the form
expert.php?exp=188
Like if the user typed in https://www.mywebsite/expert.php?exp=188
WHAT DOES NOT WORK:
simple rules like RewriteRule ^expert-([0-9]*)$ expert.php?exp=$1 [L,NC,QSA]
WHAT WORK
I have the following rewrite_rule that works only when I physically create the folder expert/ in my tree, i.e. /www/expert/
# FRIENDLY URL FOR EXPERTS PROFILE
Rewriterule ^(.*)expert\/([0-9]*)(\/[a-z0-9\-\']*)?\/?$ expert.php?exp=$2 [L,NC,QSA]
Also, for this rule to work, I had to put the <base href="/"> in the page expert.php to avoid errors with all my linked resources:
Failed to load resource: the server responded with a status of 404 ()
The server is APACHE on a shared web hosting platform named OVH.
The full code of the issue:
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
# FRIENDLY URL FOR EXPERTS PROFILE
Rewriterule ^(.*)expert\/([0-9]*)(\/[a-z0-9\-\']*)?\/?$ expert.php?exp=$2 [L,NC,QSA]
</IfModule>
Have it like this with MultiViews turned off:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# FRIENDLY URL FOR EXPERTS PROFILE
Rewriterule ^/?expert/(\d+)/?$ expert.php?exp=$1 [L,NC,QSA]
Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.
I changed from using rewrite rules to using Apaches FallbackResource after reading https://www.adayinthelifeof.nl/2012/01/21/apaches-fallbackresource-your-new-htaccess-command/. It's more of a 'if page not found, then run this page instead'
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
FallbackResource /root.php
</Directory>
The only thing I have found is that if the URL is for page that does exist - then it serves that instead of your base new base page 'root.php' in my case.
Very new to this stuff so please be gentle with me.
I'm running a server with Debian8, and using it to host my website, Teamspeak and a bit of file storage.
After moving my server files into /var/www/html and sorting out DNS-stuff my site is up and running - but the clean URL fix I used (with the .htaccess file) no longer functions. I wanted to find out if this was a mod_rewrite issue so I tried to check using a phpinfo.php file - which kept downloading in browser instead of executing.
My .htaccess file looks as follows:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
# Hide .html extension
## External Redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,NC]
## ## Internal Redirect
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html
AddHandler php5-script .php
Things I have done:
Installed PHP5.
apt-get -y install php5 libapache2-mod-php5
and then restarted Apache
service apache2 restart
I hoped this would fix the PHPInfo.php issue, but still no success, so I attempted to edit the .htaccess file using
AddHandler php5-script .php
I'm assuming this will work but my .htaccess file isn't functioning so I figured I need to sort that first.
I checked for Mod_rewrite using
a2enmod rewrite
and was informed the module was already enabled. Okay, not a mod_rewrite issue.
Further searching led me to find /etc/apache2/apache2.conf, where I changed
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
to `
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>`
Still no dice! I also checked /etc/apache2/sites-available/ and found two files, 000-default.conf (which contains none of the above code) and default-ssl.conf (same situation). I also have /sites-enabled/ which for some reason just contains a shortcut to the previous 000-default.conf file.
I'm pretty much at my wits end now, I've Google'd this stuff to death and my poor knowledge of the workings of this stuff is clearly hindering my progress. Can anyone help?
I'm on a win8 machine running XAMPP. I have a virtual host directory set up with a .htaccess file. I have been researching how to rewrite urls and I found the RewriteEngine module. In my httpd.conf apache file, the module was already enabled:
LoadModule rewrite_module modules/mod_rewrite.so
It seems like the next step was to update my .htaccess file like so:
php_value register_globals off
DirectoryIndex default.php index.php
ErrorDocument 404 /filenotfound.html
RewriteEngine On
RewriteBase /
RewriteRule ^Home$ Default.php [L]
RewriteRule ^AboutMe$ About.php [L]
RewriteRule ^Work$ Work.php [L]
RewriteRule ^Blog//Categories$ Blog/Categories.php [L]
RewriteRule ^Blog//([^/.]+)/?$ Blog/Posts.php?val=$1 [L]
I have followed a couple SO questions (config and rewriting w/ params), but am unable to get even the easiest rewrites to work. I have restarted apache a couple of times with no results.
While I'm here, this is my folder structure boiled down to everything relevant:
root
.htaccess
Blog
AuthorPanel.php
Categories.php
Post.php
Posts.php
Default.php
About.php
Work.php
And these are the rewrites I am looking to achieve (I have already tried most of them):
site.com/Default.php => site.com/Home
site.com/About.php => site.com/AboutMe
site.com/Blog/Categories.php => site.com/Blog/Categories
site.com/Blog/Posts.php?id=3&val=Android => site.com/Blog/Android
site.com//Blog/Post.php?id=4&title=Working+with+ActionBar => site.com/Blog/Working-with-ActionBar
Update 1 In httpd-vhosts.conf I even tried using the RewriteEngine on and rewrite rules, with no luck either:
<VirtualHost *>
DocumentRoot "C:/Users/ben/Documents/PHP/benWIT"
ServerName benWIT.local
<Directory "C:/Users/ben/Documents/PHP/benWIT">
RewriteEngine on
Order allow,deny
AllowOverride all
Allow from all
Require all granted
RewriteRule ^Home$ Default.php [L]
RewriteRule ^AboutMe$ About.php [L]
RewriteRule ^Work$ Work.php [L]
</Directory>
</VirtualHost>
Since you are using htaccess then you will need to make sure AllowOverride is set to All in your httpd.conf file:
AllowOverride All
This will allow you to use htaccess files. Having said that, as a general rule you don't want to use htaccess files or enable AllowOverride if you have access to the apache config files simply because it will use more resources to search the directory and find the htaccess files etc. Placing the changes into the httpd.conf file or conf.d/example_host.conf is much better.
One other note, mod_rewrite is over used and is really overkill for most purposes. I would advice you use mod_alias (see http://httpd.apache.org/docs/2.2/mod/mod_alias.html) instead. I should point out this can only be use in server configs or virtual hosts, so it will not work in a htaccess file. But it should be given preference should you have the choice between the two.
Alias /home /default.php
Alias /aboutme /about.php
Alias /work /work.php
AliasMatch /blog//([^/.]+)/? /blog/posts.php?val=$1
.. and so on.
Here is a good read on when not to use mod_rewrite:
http://httpd.apache.org/docs/2.2/rewrite/avoid.html
I have a problem of too many redirection in one cakephp site.
Here is the site: http://pexinxas24.com/
This domain is pointed to root of the site and not to app folder.
Here is the code which is placed in htaccess of the root folder.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /app/
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Here is the code which is places in the htaccess of the app folder.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /app/
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
I am not getting any typs of PHP or cakephp errors.
This site always redirect to the root of the site. Whole code was working properly on old server. I have just changed the server and getting this error.
Looks like your are using Amazon-EC2 right? ;)
Well, probably you aren't redirected for the next reasons:
mod_rewrite it isn't enabled: Look inside of your httpd.conf, you should have the next line uncommented:
LoadModule rewrite_module modules/mod_rewrite.so
You haven't enabled override in htaccess: Look inside of your http.conf for the
directive "Directory", inside you should have something like this:
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
The most important line of the previous code is AllowOverride All, on amazon instances is set to AllowOverride none by default, so you should change it to All, after that you must restart your apache server to reflect changes.
Hope it helps.
This is an old thread.
Probably if you understand about apache2 Ismael's answer is quite enough, but if you're a newbe in this subject as I am, might be useful saying that in apache2 nowadays you should "replace" the part one where he says
mod_rewrite it isn't enabled: Look inside of your httpd.conf, you should have the next line uncommented: LoadModule rewrite_module modules/mod_rewrite.so
for copying rewrite.load from mods-available to mods-enabled:
cp /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load