I have a website built in PHP. Currently my URLs look like:
http://www.domain.com/web/views/site/event.php?id=1&name=Test
I want them to be like:
http://www.domain.com/event/id/1/name/Test
How can I achieve this? I have tried multiple tutorials and have checked for answers in stackoverflow but have not been able to find a proper solution.
Any thoughts?
Thanks in advance.
Create an .htaccess file and add the following lines:
RewriteEngine On # Turn on the rewriting engine
RewriteBase /
RewriteRule ^event/id/([A-Za-z0-9-_]+)/name/([A-Za-z0-9-_]+)/?$ web/views/site/event.php?id=$1&name=$2 [NC,L]
Two things that you should keep in mind:
.htaccess should be located at your root directory
Make sure that apache has the following directive regarding your root directory:
AllowOverride All
This is how is done:
If you have root access to your server, edit the httpd.conf file, find the root <Directory> line, and change it to this:
<Directory />
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
If you don't have root access, ask your server administrator to do that for you.
Related
So today I realised my VPS still had 3 domains pointing to the origin IP - I have never worked with Apache conf files and have just attempted to add my own .htaccess from my own research:
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.infamystudio.com$
RewriteRule ^/?(.*) https://www.infamystudio.com/$1 [QSA,R=301,L]
This was placed into the var/www folder and has had no effect on rewriting other domains pointing to the origin back to my IP.
Is there any good tutorials or can anyone help me write a rewrite so I can stop this.
Your .htaccess file should be in the root folder of your website, but it will only be used by Apache if the Virtualhost configuration allow a override. So because I don't know what kind of VPS you have you have to find it yourself.
In Debian like servers this is usual in /etc/apache2/site-enable/<virtual-host>.conf (all files in site-enable are only links to the the files in the folder site-available)
Allowing rewrite for your VirtualHost
<Directory "/var/www/path/to/public">
AllowOverride All
</Directory>
If this step is missing, nothing in your .htaccess file will work. If you have already a Directory-Direktive just only add AllowOverride All You may want to restrict this later but just for testing ALL would be good.
You also can have multiple Virtual host configurations for different IP or domain names.
https://httpd.apache.org/docs/current/vhosts/examples.html
I am trying to use mod_rewrite module of Apache24 server, but I am not being able to load it. I know there have been many questions asked regarding this topic and I have gone through all of them but nothing seem to work. These are the steps that I have followed until now---
CHANGED httpd.conf file made these changes--
a. Uncommented LoadModule rewrite_module modules/mod_rewrite.so
b. Changed AllowOverride None to AllowOverride All
Restarted apache server
Checked loaded modules using command prompt command httpd -M. I can see there that the mod_rewrite module has loaded. I am attaching the image below.
But after performing all these steps I can't see mod_rewrite as loaded module in phpinfo.
As it can be seen in the above pic there is no mod_rewrite loaded module.
Also as a wild hack I even tried rewriting URLs using .htaccess file but this is not working. Apache seems to ignore .htaccess file although I have put that file inside my root directory.
Note: I am running `PHP` as an apache module
Using `WAMP` stack
Using `localhost` as server
I need this module badly for URL rewriting purposes. Can you guys suggest some other way to load this module?
I am cracking my head for the past two days. Do you think a re-installation is needed or has it got something to do with path dependencies. Any suggestion will be appreciated.
EDIT
I have tried to rewrite URL from virtual host as the answer suggests that the module is loaded and it does not depend neither on .htaccess nor on info.php.But stil it is not redirecting. I am adding the Virtual host setup below---
<VirtualHost *:80>
<Directory "/Apache24/htdocs">
Options FollowSymLinks
AllowOverride All
DirectoryIndex index.html index.php
</Directory>
ServerName localhost
DocumentRoot "/Apache24/htdocs"
ErrorLog "/Apache24/logs/error.log"
CustomLog "/Apache24/logs/access.log" combined
<directory "/Apache24/htdocs">
<IfModule rewrite_module>
Options +FollowSymlinks
RewriteEngine On
</IfModule>
<IfModule rewrite_module>
RewriteRule ^working.php fun.html
</IfModule>
</directory>
# Rewrite Rules #####################
RewriteEngine On
RewriteRule ^working.php fun.html
# end Rewrite Rules #################
</VirtualHost>
The above code does not redirect it to working.php when I try to run fun.html. It simply says the requested URL /working.php was not found on this server..
Thanks in advance!
If apachectl -M or httpd -M says the module is loaded, it is loaded. phpinfo is an external thing ran by a php script, why should you trust it over httpd own software?
If you really need to use mod_rewrite, just make sure to add RewriteEngine on before your other rewrite directives.
Note: I would really make sure I need mod_rewrite knowing what I have to configure next, in many cases it is not necessary and overused.
Very important: To configure your server, if it is your server you do not need .htaccess, and mod_rewrite does not depend on it either
in my apache sites-available/default file i had change the config to folowing:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All //originally was AllowOverride None
FallbackResource rewrite.php //i added this line, too
Order allow,deny
allow from all
</Directory>
i wanted to handle all url calls in my own rewrite.php file. this works, when i visit my site at http://192.168.1.104:4567/web/knxzkcha but doesnt work when i go for http://192.168.1.104:4567/web/. i got this problem in firefox : The connection to the server was reset while the page was loading.
the index site http://192.168.1.104:4567/web/index.php works flawlessly and shows me my index.php file. all i want is to let me show the index file when visiting the root directory, too. /var/www points to the /web directory. i have some ubuntu 12.04 server 64 LTS edition
when i rewert the config lines, the root gives me the index file by default.
You do not need to add AllowOverride All, telling apache to avoid IO by checking existence of .htaccess files in current directory and all parents directories with AllowOverride None is a good recipe for speed. Avoid .htaccess files if you can edit Apache configuration.
Now FallbackResource is a quite new feature and may have some bugs. Did you check the ErrorLog for details? Could you try that with LogLevel debug?
It seems you problems is with directories, maybe you could fix it by enforcing usage of your fallback when a Directory is requested, try to add:
DirectoryIndex rewrite.php
I am a newbie to Magento and Apache conf. I'm using Magento 1.8.0. I have configured the path so as to remove index.php from the URLs for my pages eg
from
/magento/index.php/electronics/cell-phones.html
to
/magento/electronics/cell-phones.html
However my browser requests now direct to the index.php of my server root. I have tried adding a config section for magento as shown, but no change. What am I doing wrong?
<Directory "C:/hiddenpath/apache2/htdocs/magento">
Options All
AllowOverride All
Order allow,deny
Allow from all
Deny from none
</Directory>
In your .htaccess file find Rewrite Base/ and replace it with Rewrite Base/magento if it is commented using # un comment it.
I want the server to return a specific PHP page based on the directory name without a redirect. For example:
http://www.example.com/home
should return the same result as:
http://www.example.com/index.php?page=home
but it shouldn't redirect the address
you need to create a special file called '.htaccess' and put it in your web application root directory. File content could be like this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/home$ /index.php?page=home [L]
Then configure Apache (httpd.conf, httpd-vhosts.conf) to allow your web application to use that .htaccess config file
<VirtualHost 127.0.0.1:80>
DocumentRoot "/path/to/your/webapp"
ServerName localhost
<Directory "/path/to/your/webapp">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
This is a good read.
http://httpd.apache.org/docs/current/misc/rewriteguide.html
PHP can't do this without a redirect, but there are a host of solutions:
Use mod_rewrite with apache (.htaccess)
RewriteEngine on
RewriteRule ^home$ /index.php?page=home
Another would be to add an index.php to /home thats only function is to include the document root index.php.
look at frameworks like (my favorites) CodeIngniter or Zend, but you are not limited to it or try to reinvent the wheel by buid your own router
If you are using Apache you can look into htaccess scripts. There are a ton of options available through a google search