htaccess mod_rewrite directory to variable - .htaccess

I'm having some trouble with mod_rewrite in my .htaccess file.
I've found numerous tutorials about how to do the reverse of what I'm trying to accomplish.
Basically I need a solution to change directories to get variables.
So that this:
http://example.org/page-1
turns into
http://example.org/?link_id=page-1
I think I got a code that would do something almost to what I need, but I was getting redirect loops.

Here you go:
RewriteEngine On
RewriteRule ^page-([0-9]*)$ index.php?link_id=page-$1
Note: I'm not sure that index.php is your DirectoryIndex. If it's not, you should change it according to your index file.

Related

RewriteEngine: Ignore all query strings

I would like to ignore all query-strings in my redirect. How would I achieve this?
Basically, /project/(.*) should always redirect to /?tag=project&id=$1
/project/100?shareid=fromsomeemailprovider
should end et the ? and only redirect to Id 100.
Thank You!
I tried the following:
^/project/([^/]*)[/]?(.*)$
/?tag=project&id=$1&$2
to put the query-string behind another &, but this only works if the first URL hast a / at the end of it, which it often hasn't and the RewriteRule can't detect the ? sadly.
With your shown samples, please try following htaccess rules file.
RewriteEngine ON
RewriteRule ^(project)/(.*)/?$ index.php?tag=$1&id=$2 [QSA,NC,L]
Please make sure:
You clear your browser cache before testing your URLs.
You make sure to keep your index.php file along with your htaccess rules file. Also add / in case both of these files are in root location in your system.

Set an index page for a specific folder in .htaccess

What I want to do should be quite simple: when the admin writes www.example.com/admin I want that he's addressed to www.example.com/admin/admin_index.php.
I wrote this rule and I checked on other posts here on Stackoverflow: it apparently seems to be correct, but it actually doesn't work.
RewriteEngine On
RewriteRule ^/admin/?$ /admin/admin_index.php [L,NC]
Has anyone any clue on why the redirect doesn't work, since it "stays" at www.example.com/admin outputting (obviously) the 403 error?
There is no need to use a rewrite rule. Just use DirectoryIndex directive in admin/.htaccess:
DirectoryIndex admin_index.php
This will load admin/admin_index.php when a request comes for http://domain.com/admin/

.htaccess rewrite url with parameter

I want to change my url with only the parameter. As I am passing only one parameter, so I want the sub url should be changed with my parameter name. I want change the url as the following type-
From:
http://www.xyz.com/cat.php?slag=season
to
http://www.xyz.com/season
Can anyone help me to do it. I don't know how to do it. Thanks
Options +FollowSymLinks
RewriteEngine On
RewriteBase /path/to/your/directory
RewriteRule ^(.*)cat/(.*)$ cat\.php?slag=$2&%{QUERY_STRING} [L]
put the above in your .htaccess file.. and that would do the magic..
Note :
RewriteBase /path/to/your/directory
(use this line only if your application is in any of subfolder the folder)
I suggest you obtain a copy of the .htaccess that WordPress uses. It's a quite powerful starting point that allows you to handle routing internally from within your Application - which you might feel more comfortable with, as it seems to me.

Trying to exclude css and js files with IIRF rewrite

I'm trying to get IIRF working so that it rewrites certain URLs, but ignores files like .css and .js files.
I have URLs like:
/admin/
/admin/user/
/admin/user/view-details/1
/admin/user/view-details/1?edit
Which all work, but I can't seem to get this to work:
/admin/_assets/css/admin.css
My .ini file looks like this:
RewriteRule /admin/([^/]+)/([^/]+)/([^/]+)\?edit$ /admin/index.php?edit&action=$1&sub-action=$2&id=$3 [L,I,QSA]
RewriteRule /admin/([^/]+)/([^/]+)/([^/]+)$ /admin/index.php?edit&action=$1&sub-action=$2&id=$3 [L,I,QSA]
RewriteRule /admin/([^/]+)/$ /admin/index.php?action=$1 [L,I,QSA]
RewriteRule ^/admin/(.*)$ - [L]
I've also tried:
RewriteRule (.+\.)(php|jpg|png|jpeg|gif|ttf|sql|txt|htm|zip|css)$ - [L]
The things I've tried give either a blank document when I navigate directly to the css file, or I get:
HTTP Error 404.4 - Not Found
The resource you are looking for does not have a handler associated with it.
I don't have much experience with IIRF or IIS, so I'm a bit stuck with what to try next. Am I looking in the wrong place? Or have I made a really basic mistake?
Thanks
Tip for debugging iirf: use StatusUrl to check first for problems.
I've managed to fix this by adding:
RewriteRule /admin/_assets/(.+)$ /admin/_assets/$1 [L,I,QSA]
to the top of my file. I'm still not sure why nothing is being logged, but this fixed it for my needs.

Use htaccess to mask folder name

Here's a problem I'm always wanting to solve with htaccess. I haven't found a way yet, though it looks like it should be possible - perhaps someone can help.
Let's say I have a folder at the root of my site called /foo/. I want users to be able to access that folder at the path /bar/, but for various reasons I can't rename the folder.
So as not to create confusion I only want one path to ever be seen - that is to say, I don't want people to use the name /foo/ to access the folder; they should always use /bar/. If someone goes to example.com/foo/, their browser should redirect to example.com/bar/ - but the content returned should be the content of /foo/.
To make matters more complicated, pages in /foo/ have dependencies (images, stylesheets, links to other pages, etc) within /foo/ which are hardcoded and can't be changed. These must, of course, still work.
So, to summarise, this is what I want :
Requests for example.com/foo/ should redirect to example.com/bar/.
Requests for example.com/bar/ should return the contents of example.com/foo/.
Is this possible? It looks on the surface as if it would create an infinite redirect... but I'm pretty sure there are ways to prevent that in htaccess, aren't there?
I'd be very grateful for any help.
(PS - for a little extra background: The normal reason I want to do this is to rename the wordpress /wp-admin/ directory to something more professional and easy for customers to remember, such as /admin/. But the same system should work for masking any path in this way.)
I found a sort of workaround - by using a symlink and htaccess in combination.
First I created a symlink from /bar to /foo/.
Then I put this in htaccess :
Options +FollowSymLinks
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
This has exactly the desired result - example.com/bar/ shows the content of the /foo/ directory, and example.com/foo/ redirects to example.com/bar/
But if anyone can come up with a pure htaccess solution I'd much prefer that!
Update :
Ok, I've finally found out how to do this. It turns out to be quite simple...
RewriteCond %{THE_REQUEST} ^GET\ /foo/
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
RewriteRule ^bar/(.*)$ foo/$1
The only problem is that it doesn't take account of RewriteBase, so you have to include the full path in the first line (after ^GET\).
If I understand correctly what you want is something like this inside your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^foo/$ bar/
RewriteRule ^bar/$ foo/
</IfModule>

Resources