I am making an url shortener with a base 64 encoding for my urls. problem is I also have a bunch of folders inside my main directory. My directory looks something like this
/
/css
style.css
/handlers
handle_database.php
index.php
.htaccess
And this is the rewrite rule I use to capture the encoded urls
RewriteRule ^([A-Za-z0-9_\-]{3,8})$ index.php?a=$1
And that works. my problem comes when my system generates an url that would be like this http://exampleurlshortener.com/css, in an ideal scenario the rewrite rule would capture the "css" and let my index.php handle the rest, problem is apache adds a trailing slash and it ends up getting inside the css directory.
So what I need is that
http://exampleurlshortener/css -> lets index.php handle the request
http://exampleurlshortener/css/ -> access the actual directory
So far I've had no luck, because apache keeps adding the trailing slash
You need to turn off DirectorySlash to avoid Apache adding a trailing slash. It is also important to turn off Indexes option to avoid Apache presenting directory listing to users.
DirectorySlash off
Options -Indexes
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^([\w-]{3,8})$ index.php?a=$1 [L,QSA]
Related
This looks simple but I do not know what thing is going wrong. I am using MAMP on MacOS and mod_rewrite is already enabled. Below is the .htaccess content:
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)\.html$ view.php?symbol=$1 [NC,L]
The URL http://localhost/pe/data/view.php?symbol=APPL should be accessed if someone visits: http://localhost/pe/data/APPL.html
I am getting a 404.
The htaccess file exists in /data/ folder.
The 404 results because the rule rewrites the request to /view.php (as indicated by the RewriteBase directive), not /pe/data/view.php as intended.
If the .htaccess file is in the /pe/data directory and the URLs you are requesting are relative to this directory and view.php is located in this directory (as appears to be the case) then you should... remove the RewriteBase directive entirely.
The RewriteBase directive specifies an alternative URL-path to use for relative path substitutions. view.php (without a slash prefix or scheme+hostname) is a relative path. The default path is the directory that contains the .htaccess file (the directory-prefix). So, if you are rewriting to the default path, you don't need the RewriteBase directive at all.
RewriteRule ^([a-zA-Z0-9_-]+)\.html$ view.php?symbol=$1 [NC,L]
Minor point, but the NC flag is entirely superfluous here.
I am trying to redirect one subdirectory to another using RewriteRule in the main directory's .htaccess file.
For example: http://website.com/subdir should rewrite to http://website.com/another_dir/destination_dir but the user should still see http://website.com/subdir in the address bar.
This works perfectly if the user ends the URL with a trailing slash. Example: http://website.com/subdir/ (Generates a 200
However, if the slash is omitted, a 301 redirect is generated and we see the undesired destination directory. For example, http://website/subdir redirects the user to http://website/another_dir/destination_dir
Here are the pertinent parts of .htaccess:
# URL Rewriting:
RewriteEngine On
RewriteBase /
...
# Redirect subdirectories:
RewriteRule ^subdir(.*)$ another_dir/destination_dir$1 [PT,NC,QSA,L]
Any assistance is appreciated!
You can adjust the regex in your rewrite rule to optionally match a slash.
RewriteRule ^subdir(/?.*)$ another_dir/destination_dir$1 [PT,NC,QSA,L]
Note the /? just after subdir. This says that there may or may not be a slash just after your subdir, so the regex will match either way.
A quick fix would be
DirectorySlash OFF in this htaccess, and DirectorySlash ON a .htaccess in another_dir/
I would like to have clean URLs in my projects. So I've written these codes in a .htaccess file.
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?page=$1&id=$2 [NC,QSA,L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [NC,QSA,L]
But it does not work completely when I'm trying to work with it locally.
Imagine that I have a directory myproject in htdocs (www) in my local web server path and other files are stored in this folder. Now I can see the project if I go to localhost/project.
Now I want to work with URLs.
It works well if I have only one parameter in URI like localhost/myproject/tours. But if I have 2 parameters like localhost/myprojects/tours/inside, it seems that all css, js and images files go away. I've also added RewriteBase /myproject to .htaccess file, but nothing solved.
What is my mistake? I need a solution that works on both remote and local server.
First of all, see my response on your other question about your code: Why .htaccess mod_rewrite does not show a real directory
Now, RewriteBase won't solve your problem about css/js/images etc. It's only for htaccess and it defines the base path when a rule is rewritten.
One common way to avoid this problem is to add in all your files a base url right after <head> html tag.
For you, it would be: <base href="http://localhost/myproject/" />
Otherwise, if you reach localhost/myprojects/tours/inside then your css/js/images links will be resolved as localhost/myprojects/tours/inside/__here__ because the default base path here is the current directory (/myproject/tours/inside/) and this is not what you want
Edit: if that's the case, don't forget to remove leading slashes from your css/images/javascript html links
The browser will build absolute URL paths out of your relative URL paths by looking at your made up context of /myprojects/tours. You may need to strip one or two levels of that prefix off to find the real path.
The access log will show you plain as day what relative URL's come in when you use the old and new URLs.
I have a Drupal7 site hosted in a sub-directory and site can be access following with directory path and trailing slash:
http://mydomain.com/dir1/dir2/ (with the trailing slash).
I’m looking to access this site without the trailing slash:
http://mydomain.com/dir1/dir2
I tried with the DirectorySlash Off in the .htaccess file. But it gives Access forbidden error when accessing site without trailing slash.
Then I created a new .htaccess file in the ../dir1 and tried to rewrite the url to get the main page content when user access site without trailing slash.
<IfModule mod_rewrite.c>
DirectorySlash Off
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^dir2$ dir2/index.php
</IfModule>
Again I’m getting the same issue “Access forbidden”. Is there anything to do in .htaccess file in Drupal root after setting these rules?
Any idea how to access Drupal site hosted in a sub-directory without trailing slash?
Try this. But I'm not tested it.
Basically the trick here is to use a 301-redirect to remove the trailing slash at the end of all your URL’s. The code to put in your .HTACCESS
#remove trailing slashes
RewriteCond %{HTTP_HOST} !^\.domain\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
There is a Drupal plugin also. According to their description looks like it will match to your requirement. Give a try.
https://drupal.org/project/globalredirect
i have a domain with a website in a folder.
i.e
http://domain.com.au/website/page.php
i only have access to .htaccess files to make the rules.
i would like my url to look like this..
http://domain.com.au/page
so in essence i want to drop the subfolder its sitting in, and the php extention.
All links in my php are structured like this though
page link
this is the same for js and css. they are all referenced from the 'website' folder.
will the rewrite mean i have to change all my links?
priority is dropping the folder from the url, not so much the php extension.
1) You could move all the files to the root web directory and run the website from there.
2) You would have to manually modify every link to remove the website directory from the beginning and then use the .htaccess file to redirect the pages back to the appropriate page. If you're going to do it this way, you might as well remove the PHP extension...
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^domain.com.au$ [NC]
RewriteRule ^(.*)/ website/$1.php [L]
You may have to modify the website/$1.php part to include your entire root directory in some cases, but that should work as long as the .htaccess file is in the root web directory. That will redirect any page in the form 'http://domain.com.au/page' to 'http://domain.com.au/website/page.php' while keeping the URL the same in the address bar.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^page\_one/(.*)/([0-9]{1,6})/?$ webroot/products/products/view/$2 [L]
</IfModule>
this htaccess code redirects all trafic from www.domain.com/page_one/anything/numeral
to the page www.domain.com/webroot/products/products/view/same numeral
for your example, something like
RewriteRule ^page?$ website/page.php$2 [L]
note, this works for "page" as a string, if you need other functions , change it
for more insight, you might want to take a look at this link :
http://www.javascriptkit.com/howto/htaccess.shtml