PHP Mod Rewrite Sitemap Software Not Working - php

I was looking to make the URLs on my site a bit more SEO friendly after reading a few sites about it. Basically I wanted old URLs that looked like
/showarticle.php?articleid=1
/showstory.php?storyid=1
to instead go to these urls:
/article/1/article-headline
/news/1/news-headline
I got this working by using the following mod_rewrite rule:
RewriteEngine On
RewriteBase /
RewriteRule ^article/([0-9]+)/([^/]*)$ /showarticle.php?articleid=$1 [PT] [L]
RewriteRule ^news/([0-9]+)/([^/]*)$ /showstory.php?storyid=$1 [PT] [L]
However, since implementing those sitemap software and sites don't seem to be working properly. phpsitemapNG finds just 1 page, and XML-sitemaps.com finds some but not all. On the other end of the scale though, GSiteCrawler and AuditMyPC find more than there are by replacing the headline part of the url with other stuff such as:
/news/1/contact.php
/news/1/virtualnetwork.php
/news/1/compare.php
These are all pages on my site but not in the news or article section and there's no links to any of these pages in the form that they're picking them up. Is this something to do with my rewrite rule or just something that sitemap generators do in general?

NOTE: The OP originally added this to their question. I have moved it to an answer to be in line with site guidelines.
Just a quick note to say that this issue has been sorted out. I just had to use absolute URLs to stop this from happening.

Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/?$ $1.php
RewriteRule ^/article/([A-Za-z0-9-]+)/(.*)/?$ showarticle.php?articleid=$1 [NC,L]

Related

.htaccess Rewrite - Masking Dynamic .php extensions

I understand that similar questions have been asked before in regard to .htaccess, but after hours of reading through the Questions, Answers and Comments, as well as countless hours reading .htaccess documentation and even trying .htaccess generators... and messing my site access up via trying examples... I come to seek those wiser than me for guidance.
I am using the following .htaccess file to remove .php extensions from URLS shown to the user in the browser
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
This works perfectly
www.example.com/users/player.php?id=1 is being correctly shown as
www.example.com/users/player?id=1
I am now struggling to write/find/understand examples that will allow me to rewrite the full extension
I want to showwww.example.com/users/player.php?id=1aswww.example.com/users/1
totally removing the player.php?id= part of the dynamic URL
Thank you in advance for any help/guidance.
For anyone else who wants to do what I wanted to do, I have found a solution.
Question: Htaccess rewrite with id
Answer: from Anubhava
This will allow you to externally mask "edit.php?id=1" to "edit/id/1" as well as use either URL interchangeably.
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/([^/]+)/edit\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1/edit/id/%2? [R=302,L,NE]
RewriteRule ^([^/]+)/edit/id/([^/.]+)$ $1/edit.php?id=$2 [NC,L,QSA]
Best of luck!

Creating a WordPress-like permalinks rewrite/redirect using PHP and .htaccess, is it possible?

I've been working on a module for a PHP-based application that would allow users to create custom pages. These pages have a URL structure as follows:
yourdomain.com/page.php?url=random-slug
I would like to have these URLs rewritten to use a more SEO-friendly approach:
yourdomain.com/random-slug/
In addition, there should two (2) redirects that accompany this rewrite:
yourdomain.com/page.php?url=random-slug => yourdomain.com/random-slug/
yourdomain.com/random-slug => yourdomain.com/random-slug/
Would this be possible? If so, what would the .htaccess (Apache) file look like to accomplish this? I have tried the following for the rewrite:
RewriteRule ^([^/]*)?.php$ ./page.php?url=$1 [L,NC]
Unfortunately, the above also seems to break other links on the site as well. I'm not exactly sure why yet but was hoping someone could point me in the right direction.
Thank you for your help!
Edit:
I was able to find a way to accomplish this with the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*?)/?$ ./page.php?url=$1 [L]
RewriteCond %{THE_REQUEST} /page\.php\?url=([^\&\ ]+)
RewriteRule ^/?page\.php$ ./%1/? [R=301, L]
Hopefully this will help anyone else with a similar request :)

.htaccess filename to url

I've been searching and cannot find an answer that suits my needs. It is a rather simple question I assume.
The point is that I need to rewrite my files to a name of my liking. For example I have, 'www.mydomain.com/account_index.php' which I want to rewrite to www.mydomain.com/account
Thus far I had this, but that doesn't seem to work with multiple lines.
Rewriteengine on
rewriterule ^support account_ticket.php [L]
rewriterule ^account account_index.php [L]
So in short, I want my website to redirect /account to account_index.php and the other way around.
Thanks in advance!
I found the answer for those that are wondering.
I had to put a RewriteCond before every RewriteRule.
So if I wanted to go to www.mydomain.com/account. I'd have this:
RewriteCond %{REQUEST_URI} ^/account$ [NC]
RewriteRule ^account account_index.php [NC,L]
This means that /account is now linked to account_index.php.
Have a nice day!

Virtual site architecture with PHP queries

I have searched around but I cant find a solution, that covers all of my aspect.
I want this URL
www.site.com/page/underpage
To go to:
www.site.com/?p=page_underpage
WITHOUT the url field to change.
I know that I can use 301 redirects in .htaccess for every link, but the link keeps changing in the URL.
The reason is that I like this linking much better than the PHP queries. I see that almost all other sites has it as well.
But also a last question:
Is this not a good idea? Will it effect SEO?
Try this in your htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /?p=$1_$2 [NC,L]
This internally redirects "/page/underpage" to "/?p=page_underpage" .

PHP Friendly Urls for my script

I have a site which I have converted to use cms made simple. It works perfectly and I have the friendly urls working fine too. My issue is with a little script I wrote myself and how best to integrate it. The script is a gallery script, it reads a directory and outputs a formatted gallery in html. I was planning on making it a user defined tag in cms made simple but I hit a small snag.
The gallery script needs to be able to read in two values from the url groupId and showpage.
If I am using freindly urls then the cms and use the tag I hit a snag as the cms tries to find an actual page at "www.mysite.com/gallery/mygroup/2" and then throws a 404.
basically I need
http://www.mysite.com/gallery/photogroup/2
rewritten to
http://www.mysite.com/gallery.php?groupId=photogroup&showpage=2
UPDATE
Follwoing Yuri's advice I added his rule to the htaccess. But I have hit another snag.
So for instance if we go to
http://www.mysite.com/gallery/photogroup/2
then Yuri's rule should take effect. But that path is also a correct physical directory on my site coincidentally. Is there a way to have the rewrite rule take effect instead of bringing me to a white screen browsing the files in the directory or to the forbidden screen if I have indexes turned off which I do.
Below is my htaccess
php_flag magic_quotes_gpc Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1
RewriteRule ^gallery/(\w+)/(\d+)$ gallery.php?groupId=$1&showpage=$2 [QSA,L]
So, did you try to write in .htaccess something like this?
RewriteEngine On
RewriteCond ${REQUEST_FILENAME} !-f
RewriteCond ${REQUEST_FILENAME} !-d
RewriteRule ^gallery/(\w+)/(\d+)$ gallery.php?groupId=$1&showpage=$2 [QSA,L]
sounds like a "module" to me. Maybe this Make your module use clean URLs

Categories