im new to Mod_rewrite but problem is it ruins all my ajax and php
i use windows.location object to get current location for javascript example:
var str = location.pathname;
str=str.slice(0,str.lastIndexOf('/'));
problem that this never works since i used mod_rewrite.
my .htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^cars/([^/]+)/?$ profiles.php?carname=$1 [L]
can some body lead me to way to fix this. ?
also i face same problem with lading all imgs and css/.js but i heard i can write condition on .htaccess to only redirect .php !,
I need a way to find base address for ajax calls, and other way to find base address for php.
the problem is that you are redirecting all your url to a php file and it breaks all the other assets, including css and js.
Solution:
Use a rewrite condition. this way it only redirects if the file or directory doesn't exist.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cars/([^/]+)/?$ profiles.php?carname=$1 [L]
the first line is : if file doesn't exist
the second is: if directory doesn't exist
the there will be a redirect.
Related
I'm struggling pretty hard with .htaccess. Even though I looked up quite a few solutions here, I couldn't get to setup the following:
I have a file called index.php. Inside the index.php I have a link like
Post
Clicking the link, should lead to a file in the same directory called post.php. Inside the post.php I'm grabbing the id through $GET['id'].
But I still like to display post/12345678901 as the URL.
I already tried editing the .htaccess but clicking the links leads to a 404.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^post/([^/]+) /post.php?id=$1
You have a RewriteRule but you probably need a ReWriteCondition to go with it else this rewrite will be applied to every call to a page on the apache.
Options +FollowSymlinks
RewriteEngine on
# Not a file
RewriteCond %{REQUEST_FILENAME} !-f
# not a directory
RewriteCond %{REQUEST_FILENAME} !-d
# Edited to show root location of files.
# also added NoCaseSensitivity flag.
RewriteRule ^post/([0-9]+)/? /post.php?id=$1 [NC]
Also remove the first / in the second part of the RewriteRule because that will be domain.com/post.php which might not be your actual file location.
Your HTML appears to be showing relative filepathing which is not a good idea and might come back to bite you in the future, As a recommendation every URL on the same domain on your HTML should start with a / so;
Post
Which can then be manipulated by the .htaccess to go where you need, even in another folder (not the base).
I've been trying to rewrite my URL's with a htaccess file.
My project is using my index.php as it's basic controller.
I fetch the pages from the database with the get-value "naam".
Example:
localhost/YourDesigns/YDCMS/index.php?naam=home (this shows the homepage)
localhost/YourDesigns/YDCMS/index.php?naam=about (this shows the about page)
Now i want to rewrite the URLS to be shown like this:
localhost/YourDesigns/YDCMS/home (this shows the homepage)
localhost/YourDesigns/YDCMS/about (this shows the about page)
I have done some htaccess stuff myself and i've successfully removed the "index.php" from the url, but the "naam" still remains.
How do i remove this?
This is my current htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /YourDesigns/YDCMS/index.php?naam=/$1 [L]
Thanks in advance :)
I think your way to see the process it's not totally right, if you want to show the url like you say localhost/YourDesigns/YDCMS/home you have to first replace the url inside your html content to that format, then by using a RewriteRule you call the correct path internally:
RewriteRule ^desired_url_path/([a-z0-9\-]+)$ /base_url_path/index.php?naam=$1 [L]
this way when an user click on a link the Apache server will use the above regex to convert the url by a rule that basically says : everything after the base url is aparameter value to be passed on the index.php.
Naturally the regex can be modified to suit your needs, the one i've written above it's a basic string pattern.
I know theres a lot of posts about redirects but this is a little different (I think).
Basically I want my outlinks to be example.com/out/1234 and I want them to go to a php that looks up the URL 1234 if referenced to in MySQL and the php header redirect to that URL.
The problem Im having is passing 1234 to a page. I know how if it was out.php?q=1234 but I want it to be /out/1234
Does there need to be an index file within an /out directory that also has a htaccess to rewrite it?
If so, any ideas what the regex need to be to do this? I have seen a few sites doing this and I cant work it out.
htaccess file in your document root, you can try adding:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?out/(.*)$ /out.php?q=$1 [L]
Replace the /out.php with whereever your php script for handling the URL is
I am currently working on a web application where i want to give user access to their profile by typing www.mysitename.com/username this is working fine with .htaccess but i also want the user to access other pages with clean urls by redirecting
http://www.mysitename.com/song-type-one.php?song=xyz to
http://www.mysitename.com/song-type-one.php/song/xyz
i used the rewrite rule like this
RewriteRule ^(.*)$ ./song-type-one.php?song=$1
RewriteRule ^lok-songs/(.*)$ .song-type-one.php?song=$1
if i type the clean url now in the browser only my main content is loaded the css, javascript and my php include are not loaded. there might be something wrong with my .htaccess whats the best way to achieve all rules i want with .htaccess ?
Try adding this before it:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
If the file exists on disk it won't do your rewrite rule allowing the JS and images to be loaded normally.
Im trying to pass some variables from the URL to the PHP script. Now I know that www.site.com/index.php?link=HELLO would require $_GET['link'] to get the variable "link". I was hoping there are other ways to do this without the variable.
For instance I want a link structure like this: www.site.com/HELLO. In this example I know that I have to create a Directory called Hello place an index file and it should work but I don't want to create a directory and Im hoping there's a way to "catch" that extra part after the domain. I'm thinking of creating a custom HTTP 404 Page that will somehow get the variable of the not found page, but I don't know how to get the HTTP 404 error parameters. Is there another simpler way to get a variable without the use of the extra ?link= part? I just want it to be a structure like this www.site.com/HELLO.
What you want is URL rewriting. You don't mention what kind of web server you're using, so I'll assume it's Apache.
If you have mod_rewrite enabled on your web server, this can easily be accomplished by creating a .htaccess file in your document root with the following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1
This will make all requests - that match the regular expression [a-zA-Z0-9]+ - get forwarded to index.php. For instance, if you try to access domain.com/hello, PHP would interpret this as trying to access index.php?request=hello.
You can read more about this in the Apache HTTP Server manual about mod_rewrite.
In which case, you normally use .htaccess to alter the URL in some form. For instance:
RewriteEngine On #Enable Rewrite
RewriteCond %{REQUEST_FILENAME} !-f #If requested is not an existing file
RewriteCond %{REQUEST_FILENAME} !-d #If requested is not an existing directory
RewriteRule ^(.*)$ /index.php?link=$1 [L] #Preform Rewrite
Which will make www.site.com/hello to www.site.com/index.php?link=hello. This change is invisible to the user (he will still see www.site.com/hello as an address). Be advised that it may cause trouble if you try using relative paths with CSS/JavaScript files.
You need to use URL rewriting to do this. If you're using Apache: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
you can do that with a mod-rewrite structure.
Here's a pretty good tutorial on how to set it up with php/apache.
http://www.sitepoint.com/guide-url-rewriting/