PHP rewrite rule is not working for query string - php

I am writing the following rules in htaccess file to change the query string as directory structure
RewriteRule ^dashboard/([0-9]*)?$ dashboard.php?user_id=$1
is used to rewrite the url. It is working fine on
localhost/project/dashboard // (dashboard.php)
and all links are as
localhost/project/css/style.css
localhost/project/js/script.js
But When I append an id
localhost/project/dashboard/1 // (dashboard.php?user_id=1)
It changes all the links as
localhost/project/dashboard/css/style.css
localhost/project/dashboard/js/script.js
Why it is appending the dashboard to all links

How is the style.css referenced in your html file?
If you have it like this href="css/style.css", the HTML doesn't know you're rewriting, thinks /1 is a folder and will look in dashboard/css/style.css
Any system that uses url rewrite usually has to write all the path to the styles and scripts to avoid this. so you will have to reference your style like this
href="http://localhost/project/css/style.css"
if you have a production and development environment it will help you to have a variable like
if($SERVER['SERVER_NAME']=='localhost'){
$BASE_URL = "http://localhost/project/"
}else{
$BASE_URL = "http://mydomain.com/"
}
and put that before any call to css, scripts or images
;)

It's because you "tell him" to do that.
RewriteRule ^dashboard/([0-9]*)?$ dashboard.php?user_id=$1
// ^here you tell him to print that "dashboard"
Of course other links works - you don't even match them with that rule.
I found you something here, scroll down to the title "Strip Query Strings". There, it say you to do this:
RewriteCond %{QUERY_STRING} example=
RewriteRule (.*) http://www.domain.com/$1? [R=301]
Just, of course, change that url to your own.

Related

mod_rewrite Clean URL rewriting

I'm trying to write clean url's for my website. I'm a rookie at this so forgive me. My .htaccess file currently looks like this:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-]+)$ something.php?query=$1
RewriteRule ^([a-zA-Z0-9-]+)/$ something.php?query=$1
The first rule seems to work. For example website.com/good-looking-query-string does in fact rewrite to website.com/something.php?query=ugly+looking+query+string
The second rule is where I'm having a problem. I can't get that trailing slash to work. For example website.com/good-looking-query-string/ appears to pull up the page but without any CSS rules applied. I noticed all the links end up appended to the query string also. For example the link back to index.php ends up like website.com/good-looking-query-string/index.php
I need to get that trailing slash to work. What in the world am I doing wrong?
You can combine two line in one line:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-]+)/?$ something.php?query=$1
In this case / will be optional
Your issue is client side, you are using relative paths but you need to be using Root-relative paths. A URL of:
website.com/good-looking-query-string
loads from the root. A url of:
website.com/good-looking-query-string/
loads from the good-looking-query-string directory.
So instead of href="style.css" you should have "href="/style.css"and the index issue should behref="/index.php"instead ofhref="index.php"`.
As noted in the earlier comment your regex could be simplified by adding a ? on the trailing slash to make it option. That is not your issue though, just simplifies the rules.
RewriteRule ^([a-zA-Z0-9-]+)/?$ something.php?query=$1

PHP Seo Friendly URL

I'm having some trouble using rewrite engine to achieve my goal with friendly url.
Basically I have the following structure:
- index.php
- down
- index.php
My links in main index are something like this:
download/index.php?down=FILENAME
I would like it to rewrite something like
/download/FILE-NAME ( while pointing for the index.php inside download folder ).
I would like my links in first index could be used as well /download/FILE-NAME
My actual code:
RewriteEngine On
RewriteRule download/^([a-zA-Z0-9_-]+)$ index.php?down=$1
Can anyone help me achieving this? I'm constantly getting error due to I can't do it right.
After that, how can I get the the variable $1 after transform to seo friendly urls? I will have download/FILE-NAME and I need with PHP get the info from FILE-NAME.
So basically in the main index.php I would have a link like /download/FILE-NAME that will open the down/index.php file with the FILE-NAME ( staying something like.
Try this :
RewriteEngine On
RewriteRule ^download/([a-zA-Z0-9_-]+)$ /ifanydirectory/index.php?down=$1
And you can get variable using $_GET['down'] ifanydirectory is the directory name in which index.php exists relative path from root level, if not applicable simply use /index.php
do some experiments you'll get the correct one.
RewriteCond %{REQUEST_URI} ^download/(.*)$ [NC]
RewriteRule ^(.*)$ index.php?down=$1 [L,QSA]
to catch filename in index.php:
$filename = $_GET["down"];
Maybe I've not explain it well because it doesn't work as it's supposed.
I've edited the main question but try to explain better here.
I have a main index.php with links that would be
download/FILE-1
download/FILE-2
download/FILE-3
That links should open the folder down/index.php?down=FILE-NAME.
Isn't supposed that anyone can access "down" folder directly, so I would like to transform links in my main index.php like /down/index.php?down=FILE-NAME to something like /download/FILE-NAME.

How to add a custom word to my domain URL for every request?

Consider my domain name is
www.mydomain.com
Consider a page request
www.mydomain.com/user/register
I want to add a custom word after base URL for every request inside mydomain.com.example
www.mydomain.com/customword/
www.mydomain.com/customword/user/register
Can we do this using URL rewriting in htaccess file ?
Actually it should execute 'www.mydomain.com/user/register' internally...but externally the URL should look like www.mydomain.com/customword/user/register.
You could create the directory "register", and put an index file inside it that performs the action.
That's probably the simplest way without url rewriting anyway.
UPDATE (since the question was updated)
In .htaccess:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-.]+)/user/register/?$ user/register.php?customword=$1
register.php will receive a GET request:
//User went to www.mydomain/word/user/register
echo $_GET['customword']; // will return word in this case
Make sure that you have mod_rewrite enabled :)
Yes, you can do it with htaccess
Here is an example which will add a trailing slash with url if it doesnt contain trailing slash
http://enarion.net/web/htaccess/trailing-slash/
edit formatting updated
If you are serving one site from this then the following should work:
Edit your .htaccess file to do a url rewrite
accessing www.yourdomain.com/user/registry will actually server content from www.yourdomain.com/customword/user/registry
RewriteEngine On<br>
RewriteCond %{REQUEST_URI} !^/customword/<br>
RewriteRule ^(.*)$ /customword/$1
You haven't mentioned what kind of site you;re using..eg: PHP, MVC etc as you could do similar thing in there as well.

Inserting a hash before a directory name with Apache mod_rewrite or PHP?

I have a URL that is like the following:
http://www.example.com/client/project/subdirectory/value/
I would like like a simple way to be able to change/redirect the URL to the following:
http://www.example.com/client/project/#/subdirectory/value/
Once the redirect is complete, the hash needs to be accessible via JavaScript. I'm okay with a full refresh/redirect, just ideally that I write this once and don't have to change it again.
In other words, when the site goes live, the URLs will be structured differently, so that:
http://www.example.com/subdirectory/value/
Will change to:
http://www.example.com/#/subdirectory/value/
Edit:
I have tried using this:
RewriteRule ^profile/?$ #/profile/ [ NC,L]
Which doesn't seem to do anything
Also tried this:
RewriteRule ^profile/?$ /#/profile/ [NC,L]
Which takes me to the root directory
Also tried this:
RewriteRule ^profile/?$ #/profile/ [R,NC,L]
Which adds the whole root path to the server, followed by /%23/profile/
If you have a URL like the following:
http://localhost/tests/redir/subdirectory/value/
And you want to get it redirected to:
http://localhost/tests/redir/#/subdirectory/value/
Place a .htaccess file into the directory of tests/redir with the following:
RewriteEngine On
RewriteBase /tests/redir
RewriteRule ^(.*/)$ #/$1 [R,L,NE]
And you will get the wanted redirect. The R flag plays together with the RewriteBase directive. Also the NE flag is necessary so that you can put # literally into the redirect URI.
The hash is already used by page anchor tags. You might need to replace it with an entity or pick a better character.

what is the best way to do that name?get=

ok assume i have php page
has this name name.php?get= and has get varible named get
ok
how i can make it appear like that name?get=
If you are using apache, mod_rewrite is one way to go. There is a whole bunch of mod_rewrite tricks here.
I'd seriously reconsider before using (or overusing) mod_rewrite.
In almost all of my projects I use a simple mod rewrite in the .htaccess:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./
AddHandler php5-script .php
This tells the server to forward all pages to / (index.php) unless a file otherwise exists.
In the root directory I have a folder called "views" with all of the pages that I use. E.g. the file used for /home would actually be /views/home.php. However, in the index.php I have a script that parses the user's url, checks for the file, and includes that.
$page = substr($_SERVER['REQUEST_URI'], 1);
if(!$page) :
header("Location: /home");
if(file_exists("views/$page.php")) :
include "views/$page.php";
else :
include "views/$page.php";
endif;
This creates a variable called $page that stores the value of everything in the URL after the domain name. I use a substr() function on the Request URI to get rid of the trailing forward slash (/) on the URL.
If the variable is empty, for example if the user is simply at http://example.com or http://example.com/ then it forwards them to /home, where the script then checks for the home.php file inside of the views folder. If that file exists, it includes it, and displays it to the user.
Else, the script will simply include the 404 page telling the user that the file doesn't exist.
Hopefully this helps you, and if you need any further explanation I'd be happy to help!
I think you're wanting to re-write the URL client-side, which would include mod_rewrite.
In the route of your website, create a file called .htaccess and place the following code in it:
RewriteEngine on
RewriteRule ^name?get=(.*) /name.php?get=$1
Now when you type http://www.example.com/name?get=something, it will actually map to http://www.example.com/name.php?get=something transparently for you.
As far as i could understand your question, you can not strip the file extension because otherwise it will not run. In other words, you can not change:
name.php?get=
into
name?get=
But if you mean to create links with query string values that you can put them in hyperlinks in this way:
Click here !!
If you're looking to create links using a variable '$get', then you can create the link like this:
<a href="name.php?get=$get>Link</a>
Or if you want to get the value of the query string variable, you can use this:
$get = $_GET['get']

Categories