htaccess how differentiate between folder or url? - .htaccess

I have this problem, I'm using my url like this:
If somebody is coming to the website from a referral the have something like https://myweb.site/UERHF723R so my htaccess have this:
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?ref=$1 [L,QSA]
but how I can send somebody to https://myweb.site/foldername without send them to index.php as a variable.
I think I can use something like this:
RewriteCond %{REQUEST_URI} foldername
RewriteRule .* /foldername/index.php
But I don't want to to this for every new folder, any suggestions?

You can use:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?ref=$1 [L,QSA]
In this way, you only rewrite urls that are not directories (-d) or files (-f)
The second line is therefore not mandatory in your case.

Related

Want to rewrite URL through HTACCESS

I'm sure this has been asked in other forms, but I can't for the likes of me get this thing working after googling/trying for hours.
I have a bunch of URLs like this:
www.yoursite.com/pic_box.php?pic=$
What I want is that all URLs will only be avaiable with clean URLs.
www.yoursite.com/your-title-here
Can any htaccess master help me with this?
In your .htaccess:
RewriteEngine on
RewriteRule ^picbox/(.*)$ /pic_box.php?pic=$1 [NC,L]
Will give you the ability to rename your url like that:
www.yoursite.com/picbox/your-pic-id
Try adding these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /pic_box.php?pic=$1 [L]

modifying part of URL using htaccess

I've moved my abc folder from /myproduct/abc directory to /myproduct/extensions/abc on my server.
How can I redirect all calls to http://localhost/myproduct/abc to http://localhost/myproduct/extensions/abc ?
Ex : if requested URL is http://localhost/myproduct/abc/pqr.php, it should be redirected to http://localhost/myproduct/extensions/abc/pqr.php
Basically I want .htaccess code that can be placed inside /myproduct folder and if someone requests URL like http://localhost/myproduct/abc/pqr.php then it will look for occurrence of /abc/ and replace it with /extensions/abc/
We cannot replace /myproduct/abc/ by /myproduct/extensions/abc/ as myproduct can have white-labelled to yourproduct or myproduct1 etc..
Any help would be highly appreciated. :)
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/abc/([^\.]+)\.php/? [NC]
RewriteRule .* %1/extensions/abc/%2.php [R=301,L]
Redirects
http://localhost/Any/Number/Of/Folders/abc/AnyFileName.php
To:
http://localhost/Any/Number/Of/Folders/extensions/abc/AnyFileName.php
To keep the first URL showing in the browser's addres bar, remove R=301 from [R=301,L]
NOTES:
If only folder myproduct/ is needed, like this: http://localhost/myproduct/abc/AnyFileName.php The rule will work too.
If file AnyFileName.php, pqr.php for example, exists at folder abc in the incoming URL, the rule will be skipped (Makes no sense to have it there anyway). The script has to be at folder abc in substitution URL: http://localhost/Any/Number/Of/Folders/extensions/abc/.
Vijay,
Try this:
RewriteEngine On
RewriteBase /
# REQUEST_FILENAME should not be file name
RewriteCond %{REQUEST_FILENAME} !-f
# REQUEST_FILENAME should not be directory name
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/abc/(.*)/? [NC]
RewriteRule .* %1/extensions/abc/%2 [R=301,L]
Which server are you using? Have a look at mod_rewrite in case you are using Apache 2.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Htaccess rewrite url (Shorten url)

So I have the url: myurl.com/projects/url/visit.php?link=fbehe and I want to rewrite it as so:
myurl.com/u/fbehe
But it isn't working. I am using this so far:
RewriteEngine On
RewriteRule ^/u/([^/]*)$ visit.php?link=$1 [L]
I would also like to note that I placed my htaccess file in the directory with my visit.php file. so myurl.com/projects/url/.htaccess
How could I achieve this?
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/u/([^/]+)/?$ [NC]
RewriteRule .* projects/url/visit.php?link=%1 [L]
Maps silently
http://myurl.com/u/anyvalue with or without trailing slash
To
http://myurl.com/projects/url/visit.php?link=anyvalue
Try using NC as well. Also, not sure if the absolute path is working. Try a relative if the .htaccess is in the root of your website.
RewriteRule ^/u/([^/]*)$ visit.php?link=$1 [L,NC]
do you use any framework of this? maybe u can you there functionality like routes. can rewrite your url..
Like Codeigniter,Cake
They have routes to change the url like that.

htaccess get filename without path

I'm trying to get the requested filename without the path with htaccess for a RewriteCond.
REQUEST_FILENAME returns the full absolute path, but I only need the filename like test.php
I've been searching for this a lot but couldn't find anything that helped me out.
Thanks for any responses in advance!
Edit:
Im trying to do something like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond _%{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]*)$ _$1.php [L]
The URL typed in the browser looks like this: http://example.org/test
The File that will be requestested by the RewriteRule is: http://example.org/_test.php
With RewriteCond _%{REQUEST_FILENAME}.php -f i tried to check if the file exists first
Basically I want to do this:
URI: /test/blah
Check if _test.php exists (with underscore!)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/_$1.php -f
RewriteRule ^([^/]+)$ _$1.php [L]
I changed * to + so requests for e.g. example.com/ will not redirect to _.php

Rewrite rule to hide folder, doesn't work right without trailing slash

i have a strange apache mod_rewrite problem. I need to hide a sub-directory from the user, but redirect every request to that sub-directory. I found several quite similar issues on stackoverflow, but nothing really fits, so i decided to post a new question.
My .htaccess looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)?$ foo/$1 [QSA,L]
The document-root only contains the following folder/files:
/foo/bar/index.html
I would now expect that example.com/bar and example.com/bar/ would just show me the contents of index.html.
Instead example.com/bar/ show me the content as expected but example.com/bar redirects me with a 301 to example.com/bar/foo/ an then shows the contents. I really don't get why there is a 301 redirect in this case.
When i put something this
RewriteCond %{REQUEST_URI} !^[^.]*/$
RewriteCond %{REQUEST_URI} !^[^.]*\.html$
RewriteCond %{REQUEST_URI} !^[^.]*\.php$
RewriteRule ^(.*)$ $1/ [QSA,L]
on top of that rule it seems to work, but that would require me to list every used file extension...
Is there any other way i can omit the redirect, the folder "bar" should never be seen by an outside user.
Thanks in advance!
1st rewrite rule is redirect from /foo/(.) to ($1) and second - from (.) to $1.
just idea, this has not been tested.
Better late than never...
Got it working with a simple RewriteRule which append a / to every url that doesn't have on.
# only directories
RewriteCond %{REQUEST_FILENAME} !-f
# exclude there directories
RewriteCond %{REQUEST_URI} !^/excluded-dirs
# exclude these extensions
RewriteCond %{REQUEST_URI} !\.excluded-extension$
# exclude request that already have a /
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [R=301,L]

Resources