hi guys i am trying to rewrite a url but getting errors
i would like to rewrite
www.example.com/photos.php?u=username
to
www.example.com/#username/photos
here is my code
IndexIgnore *
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^#([A-Za-z0-9]+)$ user.php?u=$1 [NC,L]
RewriteRule ^#([A-Za-z0-9]+)$/^([A-Za-z0-9]+)$ user.php?u=$1 photos.php?u=$1 [NC,L]
Your first RewriteRule looks fine, but your second RewriteRule is full of errors.
You're trying to feed the right hand of the rule with 2 different address as seen at:
user.php?u=$1 photos.php?u=$1
Your rule on the left hand have a terminator sign at the middle of the regex and after it you start a new regex which would not work as well:
^#([A-Za-z0-9]+)$/^([A-Za-z0-9]+)$
This should work for your needs and it should be placed inside your .htaccess file, which should be located inside your domain ROOT folder along with both the photos.php and the user.php files or it will not work.
IndexIgnore *
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^#([a-zA-Z0-9]+)/photos$ photos.php?u=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^#([a-zA-Z0-9]+)$ user.php?u=$1 [QSA,L]
If you have a different folder setup then update your question with more information on it so I can update the rule to fit it.
The above rules will allow you to access the photo page in the following format:
www.example.com/#username/photos
And the users page in the following format:
www.example.com/#username
Related
I'm in the process of trying to prettify my URLs
From: http://localhost/blog.php?id=1
To: http://localhost/blog/1
I've added the below to my .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^blog/([a-zA-Z0-9]+)$ blog.php?id=$1 [NC,L]
From my understanding that should now redirect when I call my "to" url in PHP header("location: /blog/".id); it takes me to the relevant page, and my $_GET['id'] should have a value. It's coming up empty
I've been messing around with a few different regular expressions, but I don't think that's the issue.
In the apache config I changed "AllowOverride" to 'All'
To check to see if the .htaccess was even being used I put in some invalid regex, and got an error in apache_error.log
I've been on a ton of different pages asking the same thing, and watched plenty of YouTube vids, but I can't see what I'm missing
Have your htaccess Rules file in following manner. Please make sure to clear your browser cache before testing your URLs.
Options +FollowSymLinks -MultiViews
RewriteEngine On
##Rules for external redirect to blog/1 here.
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/(blog)\.php\?id=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Rules for internal rewrite to blog.php file here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/([\w-]+)/?$ blog.php?id=$1 [QSA,NC,L]
##Rules for internal rewrite to index.php file here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ index.php?id=$1 [QSA,NC,L]
try this .
1.
Options +FollowSymLinks
RewriteEngine on
RewriteRule blog/id/(.*)/ blog.php?id=$1
RewriteRule blog/id/(.*) blog.php?id=$1
example url :- http://localhost/blog/id/1/
Directory Type URL
i have been trying to make my urls "pretty" / human readable, the urls at the moment are:
[BASE_URL]/?action=viewProposal&proposaltitle=tesst
I want to rewrite them to be just [BASE_URL]/tesst
I tried using the following code and modifying it but it wouldn't work, ie. it didn't redirect the pages but didn't throw any errors.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^users/(\d+)*$ ./profile.php?id=$1
RewriteRule ^threads/(\d+)*$ ./thread.php?id=$1
RewriteRule ^search/(.*)$ ./search.php?query=$1
Will the PHP GET functions still work properly as ?action defined whether its a view / edit / delete?
I am assuming base url your index.php try following rule,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ index.php?action=viewProposal&proposaltitle=$1 [QSA,L]
Hello guys can you help me how set proper url in codeigniter using multiple applications and i proper .htaccess
Folder Structure
-application
-api
-cms
-system
-api.php
-cms.php
Its works when a use this
http://localhost/kitkat_funs/cms.php/
http://localhost/kitkat_funs/api.php/
But i want this
http://localhost/kitkat_funs/cms/
http://localhost/kitkat_funs/api/
My htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Please try following steps:
In config=>config.php, replace like following.
$config['index_page'] = 'index.php';
to
$config['index_page'] = '';
Please put following code into your .htaccess file, this might help you to achieve the result.
write your project folder name as rewritebase
RewriteBase /yourprojname/
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*) index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+) index.php?/$1 [L]
*take backup of your .htaccess file.
This problem appear when you working with Codeigniter using Multiple application(and multiple index_appname.php. Ofcourse you can add new line in .htaccess everytime you add another application but that is not convenience.
The solution is using basic Regular Expression. You need to write 2 section, one is for address without slash, and the other with more than one slash after index file
Note: in this case the index.php file has prefix "index_" following with app name
# If the user types without trailing slash example www.host.com/kitkat_funs/appname
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w*)$ index_$1\.php [L,QSA]
# If the user enter in any system section with slash example www.host.com/kitkat_funs/appname/page/etc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\/(.*)$ index_$2\.php/$1 [L,QSA]
Everytime user enter address right after domain name, for example www.host.com/kitkatname/cms
.htaccess will automatically redirect to www.host.com/kitkatname/cms.php
now you can get rid .php extension on your index file
In my site when I go to URL such as my-site.com/css or my-site.com/js, then browser displays the folder listing instead of the actual page saved in the database. If I use Options All -Indexes in my htaccess then it says forbidden. I have following rule in my htaccess to load any dynamic page:
RewriteRule ^(.*)$ page.php?url=$1 [QSA,L]
Since I have also folders named css and js in my project, so I think server displays the directory content instead of dynamic page. How can I prevent this conflict. I know if I use a suffix "page" before my page name then it can be sorted out but i don't want to use any parameter before the page url.
EDIT
Here's my complete htaccess file:
# my-site.com
Options +FollowSymlinks -MultiViews
#RewriteBase /
#Enable mod rewrite
RewriteEngine On
DirectorySlash off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ page.php?category=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/([^/]+)$ page.php?category=$1&post=$2 [QSA,L]
Since /js and /css are real directories your rewrite rules don't execute because you have this condition:
RewriteCond %{REQUEST_FILENAME} !-d
which means don't run for directories.
You can tweak your .htaccess to this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !/$ %{REQUEST_URI}/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ page.php?category=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ page.php?category=$1&post=$2 [QSA,L]
I have WAMP installed, and a project at in E:/wamp/www/project/index.php Links from index.php redirect to show.php I am using the below .htaccess script to be able to make URL in show.php appear as /article-title instead of what it normally does i.e show.php?id=xxx
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (.*)$ show.php?id=$1 [QSA,L]
But, the above .htaccess file which is found in project/ along side the index/show page does not seem to work. It does not even show me the project folder, when I go to www/
I don't know where the problem is, and rewrite-module is on
Given that your article URL is like this:
http://localhost/project/1122-some-title-comes-here
You can use this rule:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^project/(\d+)-([\w-]+)/?$ /project/show.php?id=$1 [QSA,L]
If you want your articles from the root folder like this:
http://localhost/1122-some-title-comes-here
You can change the above to:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(\d+)-([\w-]+)/?$ /project/show.php?id=$1 [QSA,L]
The above conditions means, if folder, files and symbolic links are false, rewrite it.
The rule itself ^(\d+)-([\w-]+)/?$ if it start with digits and a dash and contains additional alphanumeric characters and dash and end or not with / internally redirect to:
/project/show.php?id=$1
The $1 means to pass the result of the parenthesis from the rule to the id which is (\d+) which in our URL example would mean the number 1122.
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
"The RewriteBase directive specifies the URL prefix to be used for per-directory (htaccess) RewriteRule directives that substitute a relative path."