htaccess rewrite in subdirectory - apache

This may be very noob question, but I am relatively new to web development and have googled a lot but could not found anything like mine. I have a simple htaccess.
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^stores/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ stores/profile.php?sid=$1
this works just fine like I want it
e.g stores/profile.php?sid=12 is rewritten into
stores/12/store-seo-name
and in the stores sub directory,I have a page that displays details of a product listed by each store which takes two params (store_id & product_id)
I want to rewrite it like this
item_view.php?sid=12&p_id=35 to
item/12/35/product-seo-name
I tried a lot of methods but I could not get it to work, and it gives me 404 error when I add htaccess file in the stores sub directory.
Any help would be appreciated.

Try using this rule:
RewriteEngine On
RewriteRule ^item/([^/]*)/([^/]*)/product-seo-name$ /item_view.php?sid=$1&p_id=$2 [L]
Make sure you clear your cache before testing this.

Related

Apache mod_rewrite path to query string for specific path format

I've been working on this for weeks. I want to be able to use a friendly URL to pass variables to a PHP script. For example, if someone uses this URL:
https://example.com/foo/bar/who
I would like my PHP script to receive this:
https://example.com/index.php/?var1=foo&var2=bar&var3=who
The catch is that I ONLY want to do this rewrite when there are three vars in the path. If there are fewer or more than three, I do not want to rewrite the URL.
I've seen several other explanations related to this type of rewrite but nothing quite like this.
This is almost working, but not quite. I only want the rewrite done if there's something present for those first three variables. I know this is incomplete, but with this method at least the REQUEST_URI contains the values I can parse. But again, I only want to do this when there are three vars in the path.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^index\.php/([^/]*)/([^/]*)/([^/]*)$ index.php [L]
Thank you.
You were pretty close, have it this way. Please make sure you clear your browser cache before testing your URLs. You had already created back references only thing you needed to use them, which I have added those now.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ index.php?var1=$1&var2=$2&var3=$3 [L]

1and1 .htaccess directives: explanation needed

I have no experience with modifying .htaccess file.
I'm trying to add custom error pages to my website, and I got the following template from the hosting provider (1and1). I know how to add the pages, but I would like to understand line by line what the code is doing.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /errordocument.html
ErrorDocument 400 /errordocument.html
RemoveType x-mapp-php4 .html
Thank you in advance for your help!
Let's start with line one:
RewriteEngine On - quite a simple one, it says it in the name, it enables the rewrite engine to allow us to do many things. (I wont go into detail on what all these things are)
RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d basically checks for anything that isn't a file or a directory, if these two conditions are met, it will move on to the RewriteRule if not, then nothing past this will be run.
RewriteRule (.*) /errordocument.html - This is basically telling the server that if the above conditions are met, to redirect to the error page named errordocument.html. (This coincides with your above conditions being met of course).
ErrorDocument 400 /errordocument.html - Simply put, this just tells the server that if a 400 error is received, to then display the errordocument.html page.
Finally RemoveType x-mapp-php4 .html - This is basically telling your Apache server to remove any extensions that are .html from the end of your URLs.
For more in-depth information into each of these and how extensively they can be used, take a look at the Documentation for Apache by clicking here
I hope this helps you to understand what is going on a bit better.

URL Shortener - .htaccess update

A while ago I made a little URL shortener which basically just redirects the user to the 'long url'.
The script that does as such is -cleverly- named shorter.php.
So, my problem:
When I first made the shortener, I made an .htaccess file like this:
RewriteRule ^s/(.*)$ shorter.php?u=$1 [NC,L]
Which allowed me to rewrite: doma.in/s/VAR to doma.in/shorter.php?u=VAR, which in turn redirects to the page that is linked to the VAR.
Last week I realised I could also make my URL shortener like this: dome.in/VAR
This can also be done in .htaccess, as far as I know.
The problem comes for me when I still have to support the URL with 's/' in between 'doma.in' & 'VAR'.
Help appreciated, thanks :).
Make ^s/ optional by using:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:s/)?(.+)$ shorter.php?u=$1 [NC,L,QSA]
Added 2 RewriteCond to make sure you don't rewrite existing files and directories.

Redirect (almost) all requests to the top level url

I've looked at a lot of the other mod_rewrite questions here and tried most of them, but none seem to work for me. This is what I'd like to do.
Redirect all requests like http://abc.com/foobar to http://abc.com/
EXCEPT images and js, so requests like http://abc.com/images/foo/bar or http://abc.com/js/foo/bar
The URL bar should stay the same. So while http://abc.com/foobar loads http://abc.com/, the URL should read like the former
Ports should remain intact, so http://abc.com:8080/foobar should redirect to http://abc.com:8080
This is what I have in my .htaccess file
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(images/.*|js/.*)$
RewriteRule ^(.*)$ - [L]
The condition is working well, and images and js files are loading fine. I thought the last line would redirect everything else to just the base domain, but I'm still getting 404 errors when I test it out.
I don't want to use a rule like this
RewriteRule ^(.*)$ http://abc.com/ [L]
because the domain may be different in different deployments.
I think I just have a poor understanding of how this works, but I'm just missing something small. Can someone help me get this sorted out?
This is what I ended up using
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(/*|/index.html)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^\.]+$ index.html [L]
It's not perfect, because the last rule only looks for files without a dot (.) in them. So it will not apply to http://abc.com/images/image.jpg but will apply to http://abc.com/images.
I don't really understand why I have to do it this way, but it works as it does.

"Flat links" with Apache

I'm attempting to convert URLs with GET variables from something like
http://domain.com/?username=john
to
http://domain.com/john
using following the articles here and here.
Using the example in the first article (with a slight modification) - RewriteRule .* index.php - I have gotten that to work. The first problem is, I still want to be able to access the other files in the same directory. So the closest I've gotten so far is RewriteRule index\.php/(.+) index.php?username=$1, which is still not ideal because I don't want the filename in the URL, but there's another problem. index.php is set as the DirectoryIndex, and I have no idea how to match that.
I'm still very much a beginner at configuring Apache, so any help is greatly appreciated.
Try something like that
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteCond %{QUERY_STRING} \buser=\w+
RewriteRule .* user.php [L]
put instead of user.php the name of your script.