This mod rewrite not working correctly? - apache

I have a url like this
http://example.com/view.php?id=a1b2c3
I want it to look like this
http://example.com/v/a1b2c3
I am trying this
RewriteRule ^v/?$ view.php?id=$1 [NC,L]
But it is not working and I have no idea why

Try this tweak:
RewriteEngine On
RewriteRule ^v/([^/]+)/?$ view.php?id=$1 [NE,L]
This assumes that mod_rewrite is both installed and activated for htaccess files.
If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

Related

.htaccess RewriteRule Not Working with Apache2

I know there are a million posts about this, but I have read through a lot of them and made so many changes to get this to work, but I am at a loss. I have setup a redirect rule in my apache2 server web server, but it does not redirect.
My web server root folder structure:
/var/www/example/
|
|----l
|
|---index.php
|---.htaccess
/var/www/example/l/.htaccess content is:
$ cat /var/www/example/l/.htaccess
RewriteEngine On
RewriteRule ^l/([a-zA-Z0-9]{6})$ l/index.php?redirect=$1 [R=302,L]
I have also tried putting it in the root folder /var/www/example/.htaccess
My rewrite rule appears to be working correctly from this site:
https://htaccess.madewithlove.be?share=6cae684f-740d-4add-b711-53cdb5986681
mod_rewrite.so is installed and rewrite_module is loaded.
I ran sudo a2enmod rewrite and it was turned on
APache2.conf:
$ cat /etc/apache2/apache2.conf
<Directory "/var/www/example">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
When ever I go to https://example.com/l/i4Ikn2 I just get a 404 error.
I just want to know if I am missing something or somewhere to look. I checked the apache2 error.log and there are no error in there pertaining to this.
As #mrwhite and #anubhava mentioned in the comments below my OP, I was mixing up the way my .htaccess file was being used.
I removed the sub-directory flags (the l in the pattern) and added the QSA instead of the redirect (R=302).
This was because I was using the .htaccess file in the sub-directory.
The QSA was the correct 'option' for the redirect/rewrite (still unsure of the difference).
Original:
RewriteRule ^l/([a-zA-Z0-9]{6})$ l/index.php?redirect=$1 [R=302,L]
New (working):
RewriteRule ^([a-zA-Z0-9]{6})$ index.php?redirect=$1 [QSA,L]

Apache .htaccess URL rewrite adding additional directory in URL path

I'm trying to build a URL rewrite by adding an additional directory in the URL path; for example, changing;
https://FQDN/mypage/ to https://FQDN/dev/mypage/
https://FQDN/template/ to https://FQDN/dev/template/
I have written the following .htaccess although this is having no affect. I have already enabled a2enmod rewrite.
RewriteEngine on
RewriteRule ^dev/(.+)$ /$1
Thank you #anubhava - simple fix, the rewrite was not activating due to AllowOverride None being active in the apache2.conf file. Changing this to AllowOverride All allowed the .htaccess file to be active.

Static text on rewriterule

I've been trying different ways to create the url like this:
http://www.domain.com/postal-zip/123456
I've tried:
RewriteRule ^postal-zip/([0-9]+)$ code.php?thecode=$1 [L]
and
RewriteRule ^postal-zip/([0-9]+)$ postal-zip/code.php?thecode=$1 [L]
and a few more things but I have not got the result, does anyone have an idea about this?
Your rules look okay. It sounds like mod-rewrite is not turned on.
First, before your rules, make sure you have:
Options +FollowSymLinks -MultiViews
RewriteEngine On
Next, for this to work, mod_rewrite must both be installed and activated for .htaccess files. If you are not sure, to check if mod_rewrite is even installed, look at the list of installed modules in the output of phpinfo(); By default, mod_rewrite is not enabled for .htaccess files. If you are managing your own server, open httpd.conf and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

How To Use Htacess for www.example.com/type.php?user=name

I want to redirect www.example.com/type/name to www.example.com/type.php?user=name
{
RewriteRule ^([a-zA-Z0-9_-]+)/type$ type.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/type/$ type.php?key=$1
}
Please help me about this.
Try adding this to the .htaccess file in your web document root folder (often public_html or htdocs):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^type/([^/]+)/? type.php?key=$1 [L]
This assumes that mod_rewrite is both installed and activated for .htaccess files. If you are not sure, to check if mod_rewrite is even installed, look at the list of installed modules in the output of phpinfo(); By default, mod_rewrite is not enabled for .htaccess files. If you are managing your own server, open httpd.conf and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All
try this , hope it helps :
RewriteBase /www.example.com
RewriteEngine On
RewriteRule ^type/(.*)/$ /type.php?user=$1
#OR
RewriteRule ^(.*)/(.*)/$ /$1.php?user=$2
PS : The first one works only for the url : www.example.com/type/value ,the second one works for any page in your site ex: www.example.com/anypage/value

Url masking through htaccess not working

I was trying to setup Lithium Php framework. I followed the documentation for getting started.
I have done this: *"make sure mod_rewrite is enabled, and the AllowOverride directive is set to 'All' on the necessary directories involved. Be sure to restart the server before checking things."*
Rewrite module is enabled. Verified by
apache2ctl -M
I have changed the AllowOverride in /etc/apache2/sites-available/default file.
Now when I go to http://localhost/LithiumTestApp/, the page loads but sans css, js, images etc as the links do not work.
I can't seem to find what I have done wrong.
I'm running Apache2 on Ubuntu 11.10.
Edit: Contents of .htaccess are:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Part of Lithium Framework itself. I haven't changed anything here.
I'd recommend first checking to see if your rewrite is working properly.
I usually set a rewrite log path in my vhost file (or set it in your httpd.conf or apache2.conf, whichever your OS uses).
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 5
Then do:
tail -f /var/log/apache2/rewrite.log
That will allow you to see the log contents while it's happening and refresh the page and see if everything is in order. I also recommend having a look at your Apache error log to see if there are errors in the request.
Also, make sure your resources folder has write permission.