Url rewriting for all identical filenames - php

I would like to rewrite all my URL's like so:
http://example.com/folder/40/name/test.php
and
http://example.com/folder/40/test.php
to
http://example.com/test.php
Does anyone have a solution?
thanks

You can do that by using htaccess. It will redirect all urls like
http://example.com/folder/40/name/test.php
http://example.com/folder/40/test.php
to
http://example.com/test.php
In the htaccess file add below lines
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !(img|anyother folders that you want to ignore|anyother folders that you want to ignore|...)
RewriteRule ^(.*)/(.*)/(.*)/test.php$ test.php [L]
RewriteCond %{REQUEST_FILENAME} !(img|anyother folders that you want to ignore|anyother folders that you want to ignore|...)
RewriteRule ^(.*)/(.*)/test.php$ test.php [L]
more information Htaccess Rewrites

You can easily do it by PHP but you have to include this code in both pages before opening HTML tag.
<?php
$useragent=$_SERVER['HTTP_USER_AGENT'];
header('Location: http://example.com/test.php'); ?>
Hope this will help you!

Related

htaccess mod rewrite with smf forum url not working

i have a problem with trying to redirect urls in a htaccess file
I am trying to redirect from this url below, (u=6 will be random numbers)
index.php?action=profile;u=6
to
index.php?action=pm
RewriteEngine on
RewriteRule ^index.php?action=profile;u=/([^/]*)$ index.php?action=pm
I have tried lots of different varations but I can not seem to get it to work
I have the app installed under the directory
/messages
I tried making all the changes to the htaccess file in both the /root and /messages directory, but to no aval, any help would be grateful, thanks in advance for your time
Try with:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^action=profile;u=\d+ [NC]
RewriteRule ^index\.php$ index.php?action=pm [NC,L]
Try this:
RewriteRule ^index.php\?action=profile;u=([0-9])+$ index.php?action=pm

Htaccess rewrite rule php link not working Wordpress

I've made some search about .htaccess Rewrite function, but I couldn't find any example with an html reference.
I'm trying to redirect from my example.com/products/category/ link to another page.
Here's the link what I'd like the .htaccess file to redirect:
<a href="example.com/products/category/?category=bathroom">
I'd like to achieve this style:
example.com/products/category/bathroom/
Here's my .htaccess ReWriteRule:
RewriteRule ^products/([A-Za-z0-9-]+)/?$ /products/category.php/?category=$1 [L] # Process category
Note that I'm using two different php pages for both. page-category.php and category.php
I've places it after
RewriteEngine On
RewriteBase /
but still it doesn't work.. The only time something happened when I tried some combinations, it threw back an internal server error. Is there a problem with my RegEx or am I not doing the linking right ?
EDIT: I'm using wordpress, I forgot to mention that, sorry.
You need to use Redirect 301
RedirectMatch 301 ^example.com/products/category/?category= /example/home/page-category.php
Or you can use mod_rewrite instead:
RewriteEngine On
RewriteRule ^example/products/category/?category= /example/home/page-category.php [L,R=301]
same you can do for another file.
as i understood from your question you need something like this in category directory .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) category.php?category=$1 [QSA]
Now anything that is not a directory or a file inside category directory will be rewriten like /category/bathroom will be rewriten to (without redirection) /category.php?category=bathroom

url re write issue in .htaccess file

Here is my problem
in my website base url am redirecting to an inside folder
eg: http://example.com/
to
http://onlinevyapari.com/business/ by using this code in .htaccess file
RewriteRule ^(/)?$ /business/index.php [L]
now I want to use search engine friendly url for the same website my query is like this
http://example.com/business-details.php?id=106
but I want to keep my url like this
http://example.com/business-details/106
I have done in different way like bellow
RewriteEngine on
RewriteRule business-details/id/(.*)/ business-details.php?id=$1
RewriteRule business-details/id/(.*) business-details.php?id=$1
its happening but css is not loading properly
it will be really appreciable to me if somebody help.
thank you
Have your rule like this:
RewriteEngine on
RewriteRule ^(/)?$ business/index.php [L]
RewriteRule ^business-details/(\d+)/?$ business-details.php?id=$1 [L,QSA,NC]
For solving css/js/image path issues just use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
Alternatively you can try adding this in your page's HTML header: <base href="/" /> so that every relative URL is resolved from that URL and not the current URL.
You need to add the following, before your rules, to make the rewrite rules ignore file and directory names.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Apache and Url Rewrite

I have 2 main pages. It works like so,
<html>
<body>
<div id='menu'></div>
<div id='content'><?PHP require("content.php"); ?></div>
</body>
</html>
Then, content.php pulls the "page" from the url, and checks it through a url of "good pages". If not, there is a default. Anyway, the typical url looks like this
/index.php?page=user&id=1
/?page=group&id=3
I'm trying to make it look like this
/user/1
/group/3
This is what I have for my .htaccess file, which doesn't work 100%. It pulls the correct page, but the css files doesn't seem to be uploaded. It is in /css/ in the main directory as index and content.
EDIT
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)/(.*)$ /index.php?page=$1&id=$2 [L]
I'd like the application to be scale-able if possible.
Thanks!
You will want to be using either an absolute path to your CSS, or use a base href for it, or you can prefix the path to the CSS with a / if the path to starts from the root domain.
Your .htaccess is fine, but might want some slight tweak:
RewriteEngine On
RewriteRule ^(\w)/([0-9]+)/$ /index.php?page=$1&id=$2 [L,QSA]
please try with the following code you can get the values in the $_GET['page']. Then you can split the values by / and then can get your user name,id etc
RewriteEngine On
RewriteBase /project/
RewriteRule ^([A-Za-z0-9]+){0,1}$ index.php?page=%{REQUEST_URI} [L,NC,QSA]
In the above code you may want to replace the /project/ with /. It will work perfectly
Try this .htaccess code
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/(.*)$ /index.php?page=$1&id=$2 [L]
You can now directly access your webpage via..
www.yourdomain.com/user/1
www.yourdomain.com/group/3
Please try below code in .htaccess
RewriteEngine On
RewriteRule ^user/([0-9]+)/$ /index.php?page=user&id=$2 [L,QSA]
RewriteRule ^group/([0-9]+)/$ /index.php?page=group&id=$2 [L,QSA]

How to get to php pages without using .php in URL

I have a static website with files like index.php, blog.php, contact.php etc
How can I get my website addresses to work so that www.site.com/blog takes you to blog.php?
I think htaccess could do this for me, but am a php noob!
The only alternative I currently use is to create individual folders called 'blog, contact etc' which contains another index.php file inside it
thanks
Yes, you can use mod_rewrite to rewrite all urls. The following will rewrite all non-existing files and folders to requested filename .php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
Visiting /blog and it's not an existing directory will cause this rule to rewrite it as /blog.php.
Something like this should do it.
RewriteEngine on
RewriteRule ^(.*)$ $1.php [NC]
Have a read on mod_rewrite: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
I have a static website with files like index.php, blog.php, contact.php etc
If you are generating your documents with PHP, then the site is dynamic, not static.
How can I get my website addresses to work so that www.site.com/blog takes you to blog.php?
Assuming you are using Apache, turn on MultiViews
In your .htaccess file on your server add
Options +MultiViews

Categories